I'm going to begin by assuming the integers are sorted in an increasing order.
Lets start with file operations. To read or write from some file(s) we will need to:
#include <fstream>
Now we can create variables of type fstream. A variable of type fstream is a way to access the contents of the file that variable is set up for. So how to set up an fstream variable:
fstream myFileVariableName("myFileName.myFileExtension");
In your program you need to ask the user for all the filenames. All of these names need to each go into a seperate string variable. So this means including input/output and string libraries. (String is optional because you can use char arrays, but I'm just gonna show string)
1 2
|
#include <iostream>
#include <string>
|
reading the data into an array |
Start by creating an array that is large enough to hold all the integers:
int list1[ 100000 ] //you probably need something less than 100000
Now the next goal would be to read the entire file that the user typed in earlier for the first list:
1 2 3 4 5 6
|
int i = 0;
while( list1File != EOF )
{
list1File >> list1[ i ];
i++;
}
|
And now you might be thinking "Whoa! Whoa! Slow down!"
I created an integer i to keep track of how many elements have been read so far.
And at the start 0 elements have been read.
The while loop runs until list1File reaches the end of file (EOF)
So in other words the loop runs untile there is nothing left in the file to read :)
Now inside the loop you just read one integer at a time and store it to the array.
Then increase the integer i that is keeping track of how many elements have been read so far.
In c++ arrays start at index 0, so in that example list1[0] would be the first integer in that array.
You can kind of repeat that process to read in the integers for list2; just be sure to use different variable names.
I will post more about merging the lists in a little while.
In the meantime could you try to read in all the numbers from the file?
Just let me know if you need more help reading from files.