when you are writing the file at line 16 you write a new line after every every integer, so outputfile.txt has 6 lines not 5.
the last line is a blank line.
No, the problem is line 21. EOF is not signalled until you try to read past it, which happens on line 22. But no matter what happens on line 22 you write a on line 23. That's why there are two of the last integer in the second file.
Try this instead:
1 2
while (inf >> a)
outf2 << a << endl;
This solves two problems. The first is the one indicated above. The other is more insidious: eof() should not be used alone to terminate a file handling loop. The reason is that if something were to go wrong with a read operation (say, outputfile.txt were to contain a letter instead of a number) then the badbit would be set and the eofbit could never be set --making an infinite loop. You should terminate file I/O loops with a check against good() (either explicit or implicit as I have done above).
I would have explicitly closed inf and outf2 also... but that's me.
he does write one blank line at the end of the file. So in this case he writes 5 lines of integers then a blank line. That is why when he re-reads it and then rewites it he reads 5 integers, then a blank line. when he reads the blank line, the inf >> a fails but variable a is written anyway, But as the value of a hasn't changesd he will rewrite it's current value which the value of the the last integer read.
he then writes a blank line.
For example if he tries to write integers 111, 222, 333, 444, 555:
The first output file looks like this:
111
222
333
444
555
BLANK LINE
He then re-reads and rewites it and gets:
111
222
333
444
555
555
BLANK LINE
Thanks Guys, you both were right. I do have a blank line at the end of each file as guestgulkan insists. Also, removing the .eof() function from my code as suggested by Duoas and using an ifstream object with the extraction operator resulted in my files being identical.
OK, so I could not eliminate the blank lines, but I did get identical files, no worries for today may be tomorrow.