Right, so I’ve managed to get my program to concatenate my two text files, I now need to sort the created text file so they appear in numerical order. For example say Test1.txt contains the numbers 1,2,3,4,5 and Test2 contains the numbers 4,5,6,8, myoutput.txt should have 1,2,3,4,4,5,5,6,8. I’m not massively familiar with sorting algorithms. I’m guessing I’ll have to read the output file, sort them and the write to the output file again.
Here is my current code:
#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;
int main()
{
//collecting integers from first text file//
ifstream file1("test1.txt", ios::in | ios::binary);
if(!file1)
{
cout << "Cannot open input test file 1.\n";
return 1;
}
// collecting integers from second text file//
ifstream file2("test2.txt", ios::in | ios::binary);
if(!file2)
{
cout << "Cannot open input test file 2.\n";
return 1;
}
//outputting the concactonated file to myoutput.txt//
ofstream cout("myoutput.txt", ios::out | ios::binary);
if(!cout)
{
cout << "can't open output file ";
return 1;
}
cout << file1.rdbuf();
cout << " " << flush;
cout << file2.rdbuf();
ifstream sortfile("myoutput.txt, )
return 0;
}
Read single numbers into integers, pushing them into an std::vector, then use std::sort to sort them, then write them to an output file. Each one of these steps is trivial and has been covered in a multitude of SO questions.