C ++ fgets equivalent

What is the C ++ equivalent of a C function fgets

?

I have looked getline

from ifstream

, but when it comes to the end-of-line character '\n'

, it terminates and discards it. I'm looking for a function that just ends with an end-of-line character but adds an end-of-line character to the array char

.

+2


a source to share


3 answers


You can use std::getline()

; just add the newline character yourself. For instance,



std::ifstream fs("filename.txt");
std::string s;
std::getline(fs, s);

// Make sure we didn't reach the end or fail before reading the delimiter:
if (fs)
    s.push_back('\n');

      

+3


a source


You can always put the line terminator there manually.



0


a source


Just use Cline getline and add a new line on yourself.

0


a source







All Articles