在下面的代码中:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string x = "This is C++.";
ofstream of("d:/tester.txt");
of << x;
of.close();
ifstream read("d:/tester.txt");
read >> x;
cout << x << endl ;
}
输出:
This
由于 >> 运算符读取到第一个空格,所以我得到了这个输出。我如何将该行重新提取回字符串中?
我知道这种形式的 istream& getline (char* s, streamsize n )
,但我想将其存储在一个字符串变量中。
我该怎么做?