I'm trying to merge two files that contain some numbers into a third file but I'm not getting the right result.
This is my code:
void merge(string input_file1, string input_file2, string output_file){
fstream fs1;
fstream fs2;
fstream fs3;
int n1, n2;
fs1.open(input_file1);
fs2.open(input_file2);
fs3.open(output_file);
while(fs1 >> n1 && fs2 >> n2){
if(n1 < n2){
fs3 << n1 << " ";
fs1 >> n1;
}
else{
fs3 << n2 << " ";
fs2 >> n2;
}
}
while(fs1 >> n1)
fs3 << n1 << " ";
while(fs2 >> n2)
fs3 << n2 << " ";
}
input:
input file1: 1 2 3 4 5 6 7
input file2: 34 56 77 78 88 90 100
output file: 1 3 5 7 88 90 100