I guess thats because when you open your file the next time, you don't use the iostream flag append and thats the reason your previous contents get erased.
A file which already has contents when opened for writing needs to be opned in append mode for preserving the previous contents. More information and example here.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
It would be easier to read youe header record, then read the number of students specified into a student array. After you add a new student to the array, recreate the file by outputting the header and all the students.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
that might be easier to program, but it's not a desirable solution.
If the file gets larger it will inevitably be a LOT slower than modifying the existing file.
It could also easily lead to out of memory errors.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
that might be easier to program, but it's not a desirable solution.
If the file gets larger it will inevitably be a LOT slower than modifying the existing file.
It could also easily lead to out of memory errors.
I see your point -- in a professional setting. ButI'm doing a homework for my programming class, ... tells me your apprehension is unfounded and the OPmay be using a technique beyond him at this time. I may be wrong, but it's worth pointing out.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I see your point -- in a professional setting. But
tells me your apprehension is unfounded and the OP may be using a technique beyond him at this time. I may be wrong, but it's worth pointing out.
IMO you should start people by teaching them the right way to do things, rather than teaching them a quick hack now and later trying to explain that they were taught to do things incorrectly because the right way takes a few more lines of code.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337