如何更改txt文件中的EOL编码

How to change EOL coding in txt files?

本文关键字:EOL 编码 文件 何更改 txt      更新时间:2023-10-16

我在Visual Studio 2008中用C++编写,其他库也有问题——它们不接受我用txt文件生成的行结尾(EOL)。

在使用编写文件时如何更改

std::ofstream myFile;
myFile.open("traindata.txt");
myFile << "stuff" << endl;
// or 
//myFile << "stuff" << 'n';
myFile.close();

编辑2:

好吧,我在代码中犯了一个错误:我在每次迭代中都附加了"0",所以在EOL之前有空白。

由坏。你们说得对。谢谢你的帮助。

是否可能只是不希望发生行尾序列?使用std::ios_base::binary打开文件:这会精确地关闭转换。。。除非你真的想刷新流,否则不要使用std::endl

std::ofstream myFile("traindata.txt", std::ios_base::binary);
myFile << "stuffn";

close()通常也是不必要的,除非您想检查它是否成功。

您应该以二进制模式打开文件流,以防止C++库自动转换行尾:

myFile.open("traindata.txt", std::ios_base::out|std::ios_base::binary);

这将阻止C++库将"\n"转换为操作系统特定的EOL符号(在Windows上为CR-LF)。

下载notepad++-应该可以解决问题。

cygwin 上的dos2unixunix2dos

对于Windows样式结尾使用myfile << "rn",对于UNIX样式使用myfile << "n"