Answer to Question #171215 in C++ for paokii

Question #171215
  1. Write a C++ program that will prompt the user to give three filenames (two for input, e.g., in1.txt and in2.txt and one for output e.g., out.txt). The program should read one line from the first file and one line from the second file, compare the two lines and write to the third file the two lines, having the smaller at the end. Your program should perform some error checking regarding files.
  2. Note 1: You MUST use string objects from the C++ class string, NOT C-strings (array of characters). Note 2: Assume that the two input files (e.g., in1.txt and in2.txt) have the same number of lines, as below.
1
Expert's answer
2021-03-13T12:32:40-0500
#include <stdio.h>
#include <stdlib.h>

int main()
{
// please enter the file address of your text file which two you want to merge
FILE *fp1 = fopen("F:/file1.txt", "r");
FILE *fp2 = fopen("F:/file2.txt", "r");

// file 3 is the file in which you want merge the data of the two above files
FILE *fp3 = fopen("F:/file3.txt", "w");
char c;

if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
        puts("Either file is not available or there there is something incorrect. Please check it...");
        exit(0);
}

// copy of the file 1 data to the file 3
while ((c = fgetc(fp1)) != EOF)
    fputc(c, fp3);

// copy of the file 2 data into the file 3
while ((c = fgetc(fp2)) != EOF)
    fputc(c, fp3);

printf("The data of the file1 and file2 has been merged.");

fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS