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

Jam*_*ner 2 c++ newline file output

这只是一个简单的问题.但我正在尝试将数据输出到文件,这工作正常.但是我也试图在适当的位置实现一个新的行,所以数据打印在各行上.

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

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();
Run Code Online (Sandbox Code Playgroud)

Writer.cpp

void Writer::writeEol()
{
    out << "\n" << endl;
}
Run Code Online (Sandbox Code Playgroud)

我的代码必须以这种方式实现,因为我的整个系统就是这样构建的.

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

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

    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
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*rio 8

如果没有正确的问题,我可以猜测您正在尝试学习什么,所以请随时用实际问题更新您的"问题":

你实际上是在文件中写了两个换行符,这取决于编码(即Windows或Unix换行符),你不会看到差异:

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

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

std::cout << "The result is: ";
std::cout << 50 << std::endl;
Run Code Online (Sandbox Code Playgroud)

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