The following UNIX C program is to merge the content of all txt files in the cur
ID: 3821245 • Letter: T
Question
The following UNIX C program is to merge the content of all txt files in the current directory into file merged.dat. #include void mergegile (char filename[], FILE *fp2); int main() {FILE -fp1, *fp2; char filename [100]; system ("ellipsis (1) ellipsis");/* store txt filenames into file filenames. Tmp*/fp1 = fopen ("filenames.tmp", "r"); fp2 = fopen("merged.day", "w"); while(fscan f(ellipsis (2) ellipsis) = = 1) mergefile (filename, fp2); fclose (fp1); fclose(fp2); remove("filenames.tmp"); return 0;} void mergefile (char filename [], FILE *fp2) {FILE *fp3; int ch; fp3 = fopen (filename, "r"); while ((ch = fgetc(fp3))! = EOF) ellipsis (3) ellipsis fclose(fp3);}Explanation / Answer
#include <stdio.h>
void mergefile(char filename[], FILE *fp2);
int main()
{
FILE *fp1, *fp2;
char filename[100];
system("touch merged.dat");
/* store txt filenames into file filenames.tmp */
fp1=fopen("filenames.tmp", "r");
fp2=fopen("merged.dat", "w");
while(fscanf(fp1, "%s", filename)==1)
{
mergefile(filename, fp2);
}
fclose(fp1);
fclose(fp2);
remove("filenames.tmp");
return 0;
}
void mergefile(char filename[], FILE *fp2)
{
FILE *fp3;
int ch;
fp3 = fopen(filename, "r");
while ((ch=fgetc(fp3)!=EOF))
fprintf(fp2,"%c",ch);
fclose(fp3);
}