C++一次性读取文件所有内容

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

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/85164427