C++流出新线路

发布于 2024-11-15 08:48:56 字数 700 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳男子 2024-11-22 08:48:56

如果您使用 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.

一杆小烟枪 2024-11-22 08:48:56

use

output.open("OUTPUT.TXT", ios::out);

提醒您不能使用ios::binary

use

output.open("OUTPUT.TXT", ios::out);

Remind you that ios::binary cannot be used.

绅刃 2024-11-22 08:48:56

大概。您正在读取的文件副本与程序正在写入的文件副本不同。删除该文件,然后运行您的程序。

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.

夏夜暖风 2024-11-22 08:48:56

在这种情况下,我发现最好在十六进制编辑器中查看文件,而不是在文本编辑器中查看文件,文本编辑器可以对如何呈现文本有自己的想法,它也可以很好地查找可能会绊倒您的讨厌的不可打印字符向上。

您没有说明您正在使用什么平台,但如果您在 Windows 上,我建议使用 HxD

如果您使用的是 Windows,您将看到如下内容:

Offset(h) 00       04       08       0C       10       14       18       1C

00000000  736F6D65 74657874 0D0A736F 6D657465 78740D0A 736F6D65 74657874 0D0A      sometext..sometext..sometext..

在上例中,十六进制序列 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:

Offset(h) 00       04       08       0C       10       14       18       1C

00000000  736F6D65 74657874 0D0A736F 6D657465 78740D0A 736F6D65 74657874 0D0A      sometext..sometext..sometext..

The hex sequence 0D0A is the \n character on Windows in the above example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文