Answers for "c++ file to string"

C++
2

c++ read text file to string

#include <fstream>
#include <string>
using namespace std;

int main()
{
  ifstream ifs("myfile.txt");
  //Two ways:
  
  //Assign it at initialization
  string content( (istreambuf_iterator<char>(ifs) ),
                  (istreambuf_iterator<char>()    ) );
  
  //Assign it after initialization
  content.assign( (istreambuf_iterator<char>(ifs) ),
                  (istreambuf_iterator<char>()    ) );
  return 0;
}
Posted by: Guest on February-07-2021
2

c++ file to string

string filetostring(){
	ifstream file("file", ios::binary);
    string fileStr;

    istreambuf_iterator<char> inputIt(file), emptyInputIt;
    back_insert_iterator<string> stringInsert(fileStr);

    copy(inputIt, emptyInputIt, stringInsert);

    return fileStr;
}
Posted by: Guest on June-24-2020

Browse Popular Code Answers by Language