Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Concatenate two files (lastnameCAT.c) Your next deliverable is a C program (c

ID: 3586855 • Letter: 2

Question

2. Concatenate two files (lastnameCAT.c)

Your next deliverable is a C program (call it lastnameCat) that, given two files file1 and file2, it appends the content of file2 to the end of file1, and place the results into a new file(file3).

GIven the following files:

file1 contains: "this is the content of file 1."

file2 contains: “This is the content of file 2.”

The following command will create file3:

$lastnameCAT file1 file2 file3

file3 contains: “This is the content of file 1.This is the content of file 2.”

While the following command will create file3:

$lastnameCAT file2 file1 file3

file3 contains: “This is the content of file 2.This is the content of file 1.”

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

// main will take names of the files in argv which is array of strings
int main(int argc, char** argv)
{
// these are pointers to files
FILE *f1, *f2, *fthird;
char c;

// opening first 2 files in read mode
f1 = fopen(argv[0],"r");
f2 = fopen(argv[1],"r");
// third one in write mode
fthird = fopen(argv[2],"w");

// as long as there are characters in file 1, writing to file 3
while( ( c = fgetc(f1) ) != EOF )
fputc(c,fthird);

// as long as there are characters in file 2, writing to file 3
while( ( c = fgetc(f2) ) != EOF )
fputc(c,fthird);
// closing files
fclose(f1);
fclose(f2);
fclose(fthird);

return 0;
}

file1: Chegg is good platform to learn. file2: Yes. It is! file3: Chegg is good platform to learn.Yes. It is!