如何更改txt文件中的EOL编码
How to change EOL coding in txt files?
我在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
上的dos2unix
、unix2dos
对于Windows样式结尾使用myfile << "rn"
,对于UNIX样式使用myfile << "n"
。
相关文章:
- 如何确定我已使用非编码文件到达 EOF?
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 在卡萨布兰卡形成编码参数的列表
- 使用C++进行游程编码
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 使用已使用 java 编码的 openssl 解码数据
- boost 是否有按特殊类型值编码状态"compact optional"?
- 如何在CPP的给定目录中列出UTF编码的文件名?
- 如何使用 C++将 ISO-2022-KR 编码转换为 UTF-8 编码?
- 绝对编码器十六进制输入
- 防止C++中的硬编码数字
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 使用文件内容作为硬编码字符串
- 运行长度编码程序 c++ 的问题
- 以C++为单位进行运行长度编码
- *新的编码器*格式和运算符
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 使用 IMFSinkWriter 编码的视频的播放速度会根据宽度而变化
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 如何更改txt文件中的EOL编码