Ok, That was the simple theory.
Now things get more interesting. There are various things that can cause the status of a file to change. There are various flags you can test, including
myfile.fail()
,
myfile.good()
and
myfile.eof()
.
You will need to check the status after each action. Firstly, was the file opened successfully? So check good() immediately after opening. If necessary, print out a suitable error message.
We'll assume the file opened correctly. Now what happens when the password is read? If the file is empty, the file will enter an error state. You can clear this by using
myfile.clear()
before attempting to write out the new password.
In the case of the diary.txt file, there may be the eof() (end of file) or fail() conditions to handle. You may also want set the file to write new text to the end, by using
myfile.seekp (0,ios_base::end);
See the reference section for more details,
http://www.cplusplus.com/reference/iostream/ostream/seekp/
http://www.cplusplus.com/reference/iostream/ios/good/
etc.