Getting the nth line of a text file in C ++

I need to read the nth line of a text file (e.g. textfile.findline(0) will find the first line of a text file loaded with ifstream textfile ). Is it possible? I do not need to put the contents of the file into an array / vector, I just need to assign a specific line of the text file to a variable (specifically int).

PS I'm looking for the simplest solution that will not require me to use any large external library (for example, Boost) Thanks in advance.

+6
source share
5 answers

How about this?

 std::string ReadNthLine(const std::string& filename, int N) { std::ifstream in(filename.c_str()); std::string s; //for performance s.reserve(some_reasonable_max_line_length); //skip N lines for(int i = 0; i < N; ++i) std::getline(in, s); std::getline(in,s); return s; } 
+6
source

If you want to read the beginning of the nth line, you can use stdin :: ignore to skip the first lines of n-1, and then read the next line to assign this variable.

 template<typename T> void readNthLine(istream& in, int n, T& value) { for (int i = 0; i < n-1; ++i) { in.ignore(numeric_limits<streamsize>::max(), '\n'); } in >> value; } 
+3
source

Armen's solution is the right answer, but I thought I would choose an alternative based on the idea of โ€‹โ€‹caching jweyrich. For better or worse, it is read in the entire file when building, but only retains the position of the new line (it does not save the whole file, so it plays fine with massive files.) Then you can just call ReadNthLine, and it will jump to that line right away. and read in one line you want. On the other hand, this is only optimal if you want to get only a small part of the lines at a time, and line numbers are not known at compile time.

 class TextFile { std::ifstream file_stream; std::vector<std::ifstream::streampos> linebegins; TextFile& operator=(TextFile& b) = delete; public; TextFile(std::string filename) :file_stream(filename) { //this chunk stolen from Armen's, std::string s; //for performance s.reserve(some_reasonable_max_line_length); while(file_stream) { linebegins.push_back(file_stream.tellg()); std::getline(file_stream, s); } } TextFile(TextFile&& b) :file_stream(std::move(b.file_stream)), :linebegins(std::move(b.linebegins)) {} TextFile& operator=(TextFile&& b) { file_stream = std::move(b.file_stream); linebegins = std::move(b.linebegins); } std::string ReadNthLine(int N) { if (N >= linebegins.size()-1) throw std::runtime_error("File doesn't have that many lines!"); std::string s; // clear EOF and error flags file_stream.clear(); file_stream.seekg(linebegins[N]); std::getline(file_stream, s); return s; } }; 
+2
source

It is certainly possible. There are characters (n-1) '\ n' preceding the nth line. Read the lines until you get to the one you are looking for. You can do this on the fly without saving anything other than the current line in question.

0
source

Here is a working and neat example using std::getline() :

 #include <iostream> #include <fstream> #include <string> const int LENGTH = 50; const int LINE = 4; int main() { std::ifstream f("FILE.txt"); std::string s; for (int i = 1; i <= LINE; i++) std::getline(f, s); std::cout << s; return 0; } 
0
source

Source: https://habr.com/ru/post/896425/


All Articles