My assignment is to merge two mile into one file. The program run but I keep get
ID: 673053 • Letter: M
Question
My assignment is to merge two mile into one file. The program run but I keep getting a blank screen can anyone help with correcting my program please .
For Example
File 1 File 2 OutputFile
James C Adam B Jame C
Adam B
I need help making correction to my code listed below
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream read1 ("File1.txt");
ifstream read2 ("File2.txt");
ofstream write ("Output.txt");
string line1;
string line2;
read1 >> line1;
read2 >> line2;
while ((!read1, line1, 'n'));
{
if (line1 <= line2)
{
( getline (read1, line1,' ')) ;
write << line1 << endl;
read1 >> line1;
}
else
( getline ( read2, line2, 'n'));
write << line2 << endl;
read2 >> line2;
}
while (!read1.fail())
{
write << line1;
getline (read1, line1);
}
while (!read2.fail())
{
write << line2;
getline (read2, line2);
}
read1.close ();
read2.close ();
write.close ();
return 0;
}
Explanation / Answer
#include #include int main() { // Open two files to be merged FILE *fp1 = fopen("file1.txt", "r"); FILE *fp2 = fopen("file2.txt", "r"); // Open file to store the result FILE *fp3 = fopen("file3.txt", "w"); char c; if (fp1 == NULL || fp2 == NULL || fp3 == NULL) { puts("Could not open files"); exit(0); } // Copy contents of first file to file3.txt while ((c = fgetc(fp1)) != EOF) fputc(c, fp3); // Copy contents of second file to file3.txt while ((c = fgetc(fp2)) != EOF) fputc(c, fp3); printf("Merged file1.txt and file2.txt into file3.txt"); fclose(fp1); fclose(fp2); fclose(fp3); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.