C++ 输出到文本文件时的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13415833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:16:45  来源:igfitidea点击:

C++ New line character when outputting to a text file

c++filenewlineoutput

提问by James Warner

This is just a quick question. But I am trying to output data to a file, which works fine. However I am also trying to implement a new line at the appropriate point so the data, prints on individual lines.

这只是一个简短的问题。但我正在尝试将数据输出到一个文件中,该文件工作正常。但是,我也试图在适当的点实现一个新行,以便数据打印在单独的行上。

This is how my code currently looks:

这是我的代码目前的样子:

main.cpp

主程序

Writer w("Output.txt");
    w.writeString("The number of words in the script are: ");
    w.writeInt(count);
    w.writeEol();
    w.writeString("The number of Officers in the script are: ");
    w.writeInt(ofCount);
    w.writeEol();
    w.writeString("The number of Courtiers in the script are: ");
    w.writeInt(cCount);
        w.close();

Writer.cpp

Writer.cpp

void Writer::writeEol()
{
    out << "\n" << endl;
}

My code has to be implemented this way as my whole system is built around it like this.

我的代码必须以这种方式实现,因为我的整个系统都是围绕它构建的。

So my question is, how would I adjust my Writer::writeEol() to add a new line to my output file?

所以我的问题是,我将如何调整我的 Writer::writeEol() 以在我的输出文件中添加一个新行?

My output file currently comes out like this:

我的输出文件目前是这样的:

    The number of words in the script are: 32339The number of Officers in the script are: 2The number of Courtiers in the script are: 9

Any help is greatly appreciated. :)

任何帮助是极大的赞赏。:)

回答by Mario

Without a proper question I can just guess what you're trying to learn, so feel free to update your "question" with an actual question:

如果没有正确的问题,我只能猜测您要学习的内容,因此请随时使用实际问题更新您的“问题”:

You're essentially writing two line breaks into the file, depending on the encoding (i.e. Windows or Unix line breaks) you won't see the difference:

您基本上是在文件中写入两个换行符,具体取决于编码(即 Windows 或 Unix 换行符),您不会看到区别:

std::endlwill automatically include the correct character to create a linebreak. There's no need to write "\n"as well.

std::endl将自动包含正确的字符以创建换行符。也没必要写"\n"

Also you don't have to pass std::endlto the stream unless you really want a line break at the specific position:

此外,std::endl除非您真的想要在特定位置换行,否则您不必传递给流:

std::cout << "The result is: ";
std::cout << 50 << std::endl;

Keep in mind that depending on your editor you might not see all line breaks, e.g. Windows Notepad won't show a line break with only \n. It will instead appear as an invisible character. For actual line breaks that are accepted you'd beed \r\n. In comparison, the Open Source editor Notepad++ accepts and displays both kinds of line breaks.

请记住,根据您的编辑器,您可能不会看到所有换行符,例如 Windows 记事本不会显示仅带有\n. 相反,它将显示为一个隐形字符。对于被接受的实际换行符,你会 beed \r\n。相比之下,开源编辑器 Notepad++ 接受并显示两种换行符。

回答by icabod

In your writeEol()method, you output both a newline (\n), and you write out a std::endl. These both have the effect of writing out a newline, which is what you want. However, the std::endlwill also flush the stream, which in some cases is not desireable (it could cause slowdown if the stream is flushed after every line).

在您的writeEol()方法中,您既输出换行符 ( \n),又写出std::endl. 这些都具有写出换行符的效果,这正是您想要的。但是,它std::endl也会刷新流,这在某些情况下是不可取的(如果在每行之后刷新流,可能会导致速度减慢)。

I think that your code will do what you want (albeit with two newlines), but as shuttle87 states, you only need a \nat the end of each line to achieve the desired affect.

我认为您的代码会做您想做的事情(尽管有两个换行符),但正如 Shuttle87 所述,您只需要\n在每行末尾添加一个即可实现所需的效果。