C++流出新线路
我正在尝试在 ofstream 中添加新行,但它不起作用。 写在 OUTPUT.txt 中的同一行上
std::ofstream output;
output.open("OUTPUT.TXT");
output << "sometext" << "\r\n" << "sometext" << "\r\n" << "sometext";
output.close();
我也尝试过
output << "sometext" << std::endl << "sometext" << std::endl << "sometext";
,并且
output << "sometext" << "\n" << "sometext" << "\n" << "sometext";
所有内容都
output << "sometext" << '\n' << "sometext" << '\n' << "sometext";
所有内容都写在同一行上,没有新行...我错过了什么吗?
I am trying to do a new line in a ofstream, but it does not work. Everything is written on the same line in OUTPUT.txt
std::ofstream output;
output.open("OUTPUT.TXT");
output << "sometext" << "\r\n" << "sometext" << "\r\n" << "sometext";
output.close();
I also tried
output << "sometext" << std::endl << "sometext" << std::endl << "sometext";
and
output << "sometext" << "\n" << "sometext" << "\n" << "sometext";
and
output << "sometext" << '\n' << "sometext" << '\n' << "sometext";
Everything was written on the same line, no new lines... Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 cygwin 的 g++,它 在转换“\n”时遇到一些问题到某些设置下的 Windows 风格的 CRLF,我尝试了您的代码片段的简单版本,它工作得很好。尝试在另一个文本编辑器中打开,看看问题是否仍然存在。
In case you're using cygwin's g++, it had some issues converting the "\n" to windows-style CRLF under some settings, I tried a simple version of your snippet and it worked fine. Try opening in another text editor and see if the problem persists.
use
提醒您不能使用
ios::binary
。use
Remind you that
ios::binary
cannot be used.大概。您正在读取的文件副本与程序正在写入的文件副本不同。删除该文件,然后运行您的程序。
Probably. You're reading a different copy of the file than the one your program is writing to. Delete the file, then run your program.
在这种情况下,我发现最好在十六进制编辑器中查看文件,而不是在文本编辑器中查看文件,文本编辑器可以对如何呈现文本有自己的想法,它也可以很好地查找可能会绊倒您的讨厌的不可打印字符向上。
您没有说明您正在使用什么平台,但如果您在 Windows 上,我建议使用 HxD 。
如果您使用的是 Windows,您将看到如下内容:
在上例中,十六进制序列
0D0A
是 Windows 上的\n
字符。In cases like this I find it best to look at the file in a hex editor rather than a text editor that can have its own ideas on how to render text, it also can be good for finding pesky non-printable characters that can trip you up.
You didn't say what platform you are working on, but if you are on windows I would recommend using HxD.
If you are on Windows, you will see something like this:
The hex sequence
0D0A
is the\n
character on Windows in the above example.