C++一次性读取文件所有内容要利用到stringstream类,需要加入头文件
#include<sstream>
以及ifstream的rdbuf()方法。
完整代码
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
ifstream fin("xly2016I.txt"); // filename: xly2016I.txt
stringstream buffer; // stringstream object
buffer << fin.rdbuf(); // read file content in stringstream object
string str(buffer.str()); // store file content in a string
cout << str; // output file content
return 0;
}