Reading from 2 file and Merging

Hi,

I am struggling to get my program working.

The directions are to write a program that reads sorted integers from two separate files and merge the contents of the file to an output file (third file). The only standard output will be any errors to be reported and a
“FINISHED” statement after all items have been processed.

file1.txt
2
4
6
8
10

file2.txt
1
5
11
12
15

Output.txt
1
2
4
5
6
8
10
11
12
15


This is the code I have so far, but it is not working, and I have put the two txt files in the same directory as my .cpp file. It is not working though still. What have I done wrong and how can I fix it to read these integers from the two numbers and merge the contents into a third file?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

int num1;
int num2;

ifstream inputFile;
ifstream inputFile2;
inputFile.open ("file1.txt");
inputFile2.open("file2.txt");
ofstream outputFile;
outputFile.open("output.txt");

inputFile >> num1;
inputFile2 >> num2;

while(inputFile.eof() && inputFile2.eof())
{

if (num1 < num2)
{
outputFile << num1;
inputFile >> num1;
}
else
{       
outputFile << num2;
inputFile2 >> num2;
}

}

inputFile.close();
inputFile2.close();
outputFile.close();

return 0;
}
Last edited on
while(inputFile.eof() && inputFile2.eof())

This is probably the root of your issue here. Does this statement evaluate true when you are at the end of either file or if you are not? Secondly, what if you're at the end of one file, but the other still has numbers remaining?
So how would I code that to make it work?
.eof() returns true when you actually reach the end of a file. So you want to continue reading while those two values return false therefore use the not (!) operator.

The problem you have currently is that both of the conditions in the while loop returns false && false because you just opened the file and you haven't reached the end of it, so the loop never executes.
It just outputted "124568" in the output file! Something else wrong?
Here's the problem with the 'while' condition. It can be helpful to evaluate each case individually.

When file1 is not at end of file, and file 2 is not at EOF, the codition evaluates thus:
while ( !false && !false ) = ( true && true ) = true

Now when file 1 has reached EOF and file 2 has not:
while (!true && !false ) = ( false && true ) = false

this is a very common logic error that even pros make from time to time. What you are trying to do is loop until you've reached the end of both files.. so you want to stop when eof() is true for both.
while ( !(inputFile1.eof() && inputFile2.eof()) )

There's still a big problem in your code after that one is fixed though. It'll get stuck in an endless loop, repeatedly writing '10' to the output file.

this problem is in the line:
if (num1 < num2)
Last edited on
Topic archived. No new replies allowed.