Merge two sorted files failing. What is wrong?

Hello everybody. I have to merge two sorted files. Algotrithm that i'm using is below, but it reads not all the numbers in the files, and stops, even the files contain the same number of elements. What is wrong, I can't understand. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
ifstream f1("E:\\desc1.txt");
ifstream f2("E:\\desc2.txt");
ofstream f("E:\\desc.txt");

cout <<endl<< "Nuerele sortate: " <<endl;
int n1, n2;

f1 >> n1;
f2 >> n2;
while (!f1.eof() && !f2.eof())
{
    if (n1 > n2)
    {
        cout << n1 << " ";
        f1 >> n1;
    }
    else
    {
        cout << n2 << " ";
        f2 >> n2;
    }
}


desc1.txt is:
 
99 97 95 93 91 89


and desc2.txt is:
 
100 98 96 94 92 90


and I'm getting this:
 
100 99 98 97 96 95 94 93 92
Last edited on
As soon as one of the files is empty, line 11 will be false and you will drop out of your loop. After your loop is finished, you need to find out which file is still active and dump the rest of the elements from that file.
Topic archived. No new replies allowed.