1#include <fstream>
2#include <string>
3using namespace std;
4
5int main()
6{
7 ifstream ifs("myfile.txt");
8 //Two ways:
9
10 //Assign it at initialization
11 string content( (istreambuf_iterator<char>(ifs) ),
12 (istreambuf_iterator<char>() ) );
13
14 //Assign it after initialization
15 content.assign( (istreambuf_iterator<char>(ifs) ),
16 (istreambuf_iterator<char>() ) );
17 return 0;
18}
1string filetostring(){
2 ifstream file("file", ios::binary);
3 string fileStr;
4
5 istreambuf_iterator<char> inputIt(file), emptyInputIt;
6 back_insert_iterator<string> stringInsert(fileStr);
7
8 copy(inputIt, emptyInputIt, stringInsert);
9
10 return fileStr;
11}