【发布时间】:2016-04-02 17:23:22
【问题描述】:
文本文件如下所示:
this is a test \n to see if it breaks into a new line.
c++ 看起来像这样:
string test;
ifstream input;
input.open("input.txt");
getline(input, test);
如果您将“测试”写入输出文件,它看起来像这样:
this is a test \n to see if it breaks into a new line.
我希望它在写入文件时遇到“\n”字符时换行。
【问题讨论】:
-
\n仅在源代码的字符串文字中写入时才具有“换行符”的含义。在文本文件中(或基本上在其他任何地方),它本身就是一个反斜杠,后跟一个 n。在输入文件中获取换行符的最简单方法是按 Enter ;) -
是的,我尝试将它作为一个变量存储在一行中,然后可以将其写成两行。
-
@Jay 两个答案等着你。我会说,你应该阅读并接受一份。