#include #include using namespace std; int main(int argc, char* argv[]) { if (argc != 3) { cerr << "ERROR: You must specify exactly two files on the command line." << endl; return 1; } ifstream f1( argv[1] ); if ( ! f1 ) { cerr << "ERROR: '" << argv[1] << "' could not be opened." << endl; return 1; } ifstream f2( argv[2] ); if ( ! f2 ) { cerr << "ERROR: '" << argv[1] << "' could not be opened." << endl; f1.close(); return 1; } // We only need two integer memory space to solve the problem, // so we can handle files many times larger than internal memory // (Filling a vector will be bad if files are 1000TB large.) int i1, i2; // Read first two from each file to know where to start f1 >> i1; f2 >> i2; // Stop reading once one file reach the end while ( f1 && f2 ) { if ( i1 < i2 ) { cout << i1 << endl; f1 >> i1; } else { cout << i2 << endl; f2 >> i2; } } // Dump the remaining items from the larger file while ( f1 ) { cout << i1 << endl; f1 >> i1; } while ( f2 ) { cout << i2 << endl; f2 >> i2; } f2.close(); return 0; }