fgets equivalent in C++
What is the C++ equivalent to the C function fgets
?
I have looked at getline
from ifstream
, but when it comes to an end of line character, '\n'
, it terminates at and discards it. I am looking for a function that just terminates at the end li开发者_Go百科ne character but adds the end of line character to the char
array.
You can still use std::getline()
; just append the newline character yourself. For example,
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');
You could always put the line terminator there by hand afterwards.
Just use C++'s getline and add the newline on yourself.
精彩评论