Why are the files not identical

The files outputfile.txt and outputfile2.txt are supposed to be identical. Can you please tell me what I am doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
    file outputfile.txt is suppose to be
    identical to outputfile2.txt    */

#include <iostream>
#include <fstream>

using namespace std;

int main(){
     int a;
     ofstream outf("outputfile.txt");
     cout<<"enter 5 integers: ";
     for(int i=1;i<6;i++){
        cin>>a;
        outf<<a<<endl;
      }
     outf.close();
     ifstream inf("outputfile.txt");
     ofstream outf2("outputfile2.txt");
     while(!inf.eof()){
        inf>>a;
        outf2<<a<<endl;
     }
     return 0;
 }
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.
Thanks, but how can I remedy it. I don't need a blank line at the end of my files which seems to be the case.
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.

Hope this helps.
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.
If you really want to get rid of those blank lines, you'll have to test to see if you are writing the final item or not:
1
2
3
4
5
     for(int i=1;i<6;i++){
        cin>>a;
        outf<<a;
        if (i < 5) outf<<endl;  // write newline for all but the last line
      }

Likewise for your other loop.

You are typically better off being able to recognize and discard blank lines though, as it is the more natural file structure.
Last edited on
That is a very ingenious way of solving the problem. By the way, in what situation is the function .eof() useful.
Use .eof() to disambiguate between failure and EOF.
A simplistic example:
1
2
3
4
5
6
7
8
9
10
11
// read a file of numbers
vector <int> numbers;
ifstream inf( ... );
int i;
while (inf >> i)
  numbers.push_back( i );
if (!inf.eof())
  {
  cerr << "read failure\n";
  return 1;
  }
Topic archived. No new replies allowed.