C++ 如何去掉文件末尾的换行符

如题所述

第1个回答  2017-01-03
读取文件到内存
http://www.cplusplus.com/reference/fstream/fstream/open/
#include <fstream> // std::fstream

int main () {

std::fstream fs;
fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);

fs << " more lorem ipsum";

fs.close();

return 0;
}

这是例子
打开文件流后读取每一行数据到内存,用getline()函数就行
再删除最后一个换行符写进文件就行本回答被网友采纳
第2个回答  2020-04-24
ifstream file("test.txt", ios::in);
string buf;
while (!file)
{
getline(file, buf);
if (!file) //注意这个判断,发现是末尾就不输出换行符到文件
{
file_write << buf << endl;
}
else file_write << buf;
}
希望能帮助到你
相似回答