In the following program I am trying to understand how to read and write files.
#include
#include
using namespace std;
int main
The file will need to be opened for both read and write (I'm sorry, ignore that; fstream is opened for both read & write by default). After writing (and flushing the output), you will need to seekg()
back to the beginning of the file, or you will just be trying to read what comes after the last thing you wrote, which will of course be nothing.
myfile<<"test1 writing files"<<" ";
myfile.flush();
myfile.seekg(0, ios_base::beg);
myfile>>str1;
seekg
is used to change the position you read (get) from the file. seekp
is used to change the position you write (put) to the file.