#include <iostream>
#include <fstream> //the header file needed for writing to files
usingnamespace std;
int main()
{
ofstream myfile("example.txt",ios::out); //an object of type ostream(for outputting into example.txt)representing
//the file itself in your code. ie,whatever you do to it, will affect the file
string str;
cout<<"Enter text to be put in example.txt: ";
getline(cin,str); //user inputs into str
myfile<<str; //just like using cin or cout, you put what is in str into myfile
myfile.close();
cin.get();
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main() {
ifstream myfile( "example.txt", ios::in ); // an object of type istream, used
// for reading from a file
string str;
getline( myfile, str ); // content from file saved in str
myfile.close(); // close file so it can be used by other applications
cout << str << endl;
cin.get();
return 0;
}