Hello. I am asking for help. I have 2 input files containing sorted integers. These 2 have to be concatenated into a third file and now all the integers must be sorted. (Example: file1 contains 1 5 8, file2 - 2 3 6 and file3 must contain 1 2 3 5 6 8 ). How can this be accomplished? Thanks in advance. Here's what I have done so far:

#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
    int n;
    ifstream f1;
    f1.open(argv[1]);
    if (f1.fail())
    { cout << "Error opening " << argv[1] << endl; return 1; }
    
    ifstream f2;
    f2.open(argv[2]);
    if (f2.fail())
    { cout << "Error opening " << argv[2] << endl; return 1; }
    
    ofstream f3;
    f3.open(argv[3]);
    
    f1.close();
    f2.close();
    f3.close();	
    return 0;
}

Recommended Answers

All 2 Replies

Hello. I am asking for help. I have 2 input files containing sorted integers. These 2 have to be concatenated into a third file and now all the integers must be sorted. (Example: file1 contains 1 5 8, file2 - 2 3 6 and file3 must contain 1 2 3 5 6 8 ). How can this be accomplished? Thanks in advance. Here's what I have done so far:

A concatenation would result in 1 5 8 2 3 6 whereas what you want is a merge. Merge sort is not necessary.

Read value from file 1 (X1)
Read value from file 2 (X2)
LOOP until an EOF is reached
    if X1 < X2
        Write X1
        Read another X1
    else
        Write X2
        Read another X2
    endif

Once you have an EOF condition, you need to decide what additional code you will need. There is more...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.