3

与 C 函数等效的 C++ 是什么fgets

我看过getlinefrom ifstream,但是当涉及到行尾字符时'\n',它会终止并丢弃它。我正在寻找一个仅在结束行字符处终止但将行尾字符添加到char数组的函数。

4

3 回答 3

3

您仍然可以使用std::getline(); 只需自己附加换行符。例如,

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');
于 2010-05-16T23:28:26.350 回答
0

之后你总是可以用手把线路终结器放在那里。

于 2010-05-16T23:27:12.143 回答
0

只需使用 C++ 的 getline 并自己添加换行符。

于 2010-05-16T23:27:47.133 回答