C++ファイルのすべての内容を一括して読み込む

675 ワード

C++ファイルを一括して読み込むすべての内容をstringstreamクラスに利用するには、ヘッダファイルを追加する必要があります
#include

およびifstreamのrdbuf()メソッド.
完全なコード
#include
#include
#include
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;
}