开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜