c++ get string from text file code example
Example 1: c++ read text file to string
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream ifs("myfile.txt");
string content( (istreambuf_iterator<char>(ifs) ),
(istreambuf_iterator<char>() ) );
content.assign( (istreambuf_iterator<char>(ifs) ),
(istreambuf_iterator<char>() ) );
return 0;
}
Example 2: read text from file c++
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}