问题描述
这只是一个简单的问题。但我试图输出数据到一个文件,这很好。
然而,我还试图在适当的点实现一个新行,所以数据,打印在单独的行。
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.
所以我的问题是,我如何调整我的Writer :: writeEol()以添加一个新行到我的输出文件?
So my question is, how would I adjust my Writer::writeEol() to add a new line to my output file?
我的输出文件目前是这样的:
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
帮助非常感谢。 :)
推荐答案
没有正确的问题,我可以猜测你想学什么,所以随时更新你的问题与一个实际的问题:
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:
你本质上是写两个换行符到文件,这取决于编码(即Windows或Unix换行符),你不会看到区别:
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:
std :: endl
会自动包含正确的字符来创建换行符。也不需要写\\\
。
std::endl
will automatically include the correct character to create a linebreak. There's no need to write "\n"
as well.
code> std :: endl ,除非你真的想在特定位置换行:
Also you don't have to pass std::endl
to the stream unless you really want a line break at the specific position:
std::cout << "The result is: ";
std::cout << 50 << std::endl;
请记住,根据您的编辑器,您可能不会看到所有换行符。 Windows记事本不会只用 \\\
显示换行符。它将改为显示为不可见字符。对于接受的实际换行符,您将得到
\r\\\
。相比之下,开源编辑器Notepad ++接受并显示两种换行符。
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.
这篇关于C ++输出到文本文件时的新行字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!