是否可以在c ++中存储从文本文件中读取的换行符?

时间:2016-04-02 17:23:23

标签: c++ file io

文本文件如下所示:

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);

如果你对输出文件写'test',它看起来像这样:

this is a test \n to see if it breaks into a new line.

我希望它在遇到'\ n'字符时突破到一个新行,因为它被写入文件。

2 个答案:

答案 0 :(得分:0)

这样做:

#include <fstream>
using namespace std;

int main() {
  fstream file;
  file.open("test.txt");
  string test = "this is a test \n to see if it breaks into a new line.";
  file << test;
  file.close();
  return 0;
}

“text.txt”的结果:

this is a test
 to see if it breaks into a new line..

灵感来自:adding a newline to file in C++

答案 1 :(得分:0)

为了获得乐趣,为了获得最高效率,我们可以消除对ID | sample_id | test_id | result_id的需求,并且无需在编译时通过这样做来计算字符串的长度:

std::string