This post here is mostly giving you the answer, so I recommend trying to figure it out yourself through Duoas's reply first. If you can't figure it out, this can be used as a hint.
First of all, you'll need to use a std::ifstream object.
You start of by creating one of those objects, which would be done like this:
ifstream fin("input1.txt");
The parameter of the constructor is obviously which file you want to read information from. You can also add more parameters later on, although these aren't necessary for reading such simple files.
Now that we've got our std::ifstream object, we have multiple ways we can tackle this problem. The solution I am showing you is really simple,
although it could be something your class hasn't learned yet.
Instead of manually parsing every line, we could just create a std::stringstream object which handles all of our reading.
stringstream buffer;
This will also bring up another dependency. Include the "sstream" file.
#include <sstream>
Now, we should put all of the contents of the std::ifstream object into our std::stringstream object. You can treat std::stringstream just like you treat any other stream.
You can use the rdbuf() function in order to get the contents of the file. This would be typed like this:
buffer << fin.rdbuf();
Now close the std::ifstream and open it for the other file.
1 2
|
fin.close();
fin.open("input2.txt");
| |
Repeat the process.
1 2
|
buffer << fin.rdbuf();
fin.close();
| |
Now you need to open your std::ofstream object again. I am guessing the desired name of your file is "output.txt", and therefore I opened the std::ofstream for that file.
fout.open("output.txt");
Now we'll want to put all of the contents of our std::stringstream into the std::ofstream object. This can, once again, be done with rdbuf().
1 2
|
fout << buffer.rdbuf();
fout.close();
| |
And now it should be working. I tested it myself, and it seems to work.