Hi Can someone help me with this code for C using Code Block
Hi,
Can someone help me with this code for C++ (using Code Blocks)
Write a program that reads two input files whose lines are ordered by a key data field. Your program should merge these two files, writing an output file that contains all lines from both files ordered by the same key field. As an example, if two input files contain student names and grades for a particular class ordered by name, merge the information as shown below.
Using a text editor, create File 1 and File 2. You must read one line of a file at a time and either write it or the last line read from the other data file to the output file. A common merge algorithm is the following:
         Write the line from file 2 to the output file and read a new line from file 2.
 Write the remaining lines (if any) from file 1 to the output file.
 Write the remaining lines (if any) from file 2 to the output file.
You must write the merge algorithm yourself, do not use any code from the standard template library.
Turn in:
Analysis identifying program inputs, outputs, equations, and constants.
Test data
Source code
| File 1 | File 2 | Ouput File | 
| Adams C | Barnes A | Adams C | 
| Jones D | Johnson C | Barnes A | 
| King B | Johnson C | |
| Jones D | ||
| King B | 
Solution
#include <iostream>
 #include <fstream>
 #include <string>
 using namespace std;
int main () {
 string line1,line2;
 ifstream myfile1(\"file1.txt\");
 ifstream myfile2(\"file2.txt\");
 ofstream myfile3(\"output.txt\");
 if (myfile1.is_open()&&myfile2.is_open()) //checking if both files are exists?
 {
 while ( getline (myfile1,line1)&&getline(myfile2,line2)) //getting lines from each files
 {
    if(line1.compare(line2)<0) //compareing then for alphabetical order
    {
        myfile3 << line1 << \'\ \'; //copying them in order
        myfile3 << line2 << \'\ \';
        }
        else
        {
            myfile3 << line2 << \'\ \';
        myfile3 << line1 << \'\ \';
        }
   
 }
 //checking if any of the files still have extra lines to another and copying tham
 if(!myfile1.eof())
 {
    myfile3 << line1 << \'\ \';
    while ( getline (myfile1,line1))
 {
    myfile3 << line1 << \'\ \';
 }
    }
 if(!myfile2.eof())
 {
    myfile3 << line2 << \'\ \';
    while ( getline (myfile2,line2))
 {
    myfile3 << line2 << \'\ \';
 }
    }
 myfile1.close();
 myfile2.close();
   
 }
else cout << \"Unable to open files or files may not exist\";
 cout<<\"Done\";
 return 0;
 }
//output file content will be:
Adams C
 Barnes A
 Johnson C
 Jones D
 King B

