Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <iomanip>
- #include <vector>
- #include <algorithm>
- #include <string>
- using namespace std;
- int main(){
- int i, j, k, n = 0, n1, n2, n3, pos;
- std::vector<int> array1, array2, array3;
- std::string inname1 = "HW11F1.txt";
- std::string inname2 = "HW11F2.txt";
- std::string inname3 = "output.txt";
- cout << "Filenames are restricted to 12 characters" << endl;
- cout << "Enter the first of two input file names: ";
- cin >> inname1;
- cout << endl;
- cout << "Now a second input file name: ";
- cin >> inname2;
- cout << endl;
- cout << "Enter the output file name." << endl
- << "WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED.";
- cin >> inname3; cout << endl;
- std::ifstream infile1 (inname1.c_str());
- std::ifstream infile2 (inname2.c_str());
- if (!infile1 or !infile2) {
- cout << "There was a problem opening a file.";
- return 0;
- }
- pos = 0; i = 0;
- while (infile1 >> i) {
- array1.resize(pos+1);
- array1[pos] = i;
- pos++;
- }
- pos = 0; i = 0;
- while (infile2 >> i) {
- array2.resize(pos+1);
- array2[pos] = i;
- pos++;
- }
- n1 = array1.size();
- n2 = array2.size();
- n3 = n1 + n2;
- array3.resize(n3);
- i = 0; j = 0; k = 0;
- while (i < n1 && j < n2) {
- if (array1[i] <= array2[j]) {
- array3[k++] = array1[i++];
- } else {
- array3[k++] = array2[j++];
- }
- }
- while (i < n1) array3[k++] = array1[i++];
- while (j < n2) array3[k++] = array2[j++];
- std::ofstream outfile (inname3.c_str());
- outfile.open(inname3.c_str());
- i = 0;
- while(i<n3){
- outfile << array3[i] << endl;
- i++;
- }
- cout << "Contents of file " << infile1 << " are: ";
- for(i = 0; i < array1.size(); i++){
- cout << array1[i] << endl;
- }
- cout << "Contents of file " << infile2 << " are: ";
- for(i = 0; i < array2.size(); i++){
- cout << array2[i] << endl;
- }
- cout << "Contents of merged file, " << outfile << " are: ";
- for(i = 0; i < array3.size(); i++){
- cout << array3[i] << endl;
- }
- infile1.close();
- infile2.close();
- outfile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment