C ++ fstream function that reads a string without extraction?

In C ++, is there a function in the fstream library (or any library) that allows me to read the line delimited by '\ n' without extraction?

I know that the peek () function allows the program to "look" at the next character, reading it without extracting it, but I need the peek () function, which does this, but for the whole line.

+9
source share
1 answer

You can do this with a combination of getline , tellg and seekg .

 #include <fstream> #include <iostream> #include <ios> int main () { std::fstream fs(__FILE__); std::string line; // Get current position int len = fs.tellg(); // Read line getline(fs, line); // Print first line in file std::cout << "First line: " << line << std::endl; // Return to position before "Read line". fs.seekg(len ,std::ios_base::beg); // Print whole file while (getline(fs ,line)) std::cout << line << std::endl; } 
+12
source

All Articles