i think you know about the put-pointer.
it states where the next output operation will take place.
you must know that myfile
is a totally different object than the one in hi()
each time you call hi()
, the myfile
is constructed again and opens the file for output operations, this removes previous data stored in that file and then writes the word Hi
to that file.
after the 4 calls to hi()
, you try to write the word Hello
to that file, the problem is you're trying to write it via the stream inside main()
, this stream's put-pointer still points to the beginning of the file, that means the word Hello
will overwrite the previous word existed in that file.
for the sake of demonstration, consider switching the work of hi()
, let hi()
print the word Hello
4 times then writes the word Hi
to the file, just as follows:
for (int i = 0; i<3; i++)
the output file shall now contain:
hope that was clear enough.