27. write a C program to merge the two files


Program to merge the two files

Source Code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1 = fopen("/home/smec/Desktop/file1.txt", "r");
FILE *fp2 = fopen("/home/smec/Desktop/file2.txt", "r");
FILE *fp3 = fopen("/home/smec/Desktop/merge.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
        puts("Could not open files");
        exit(0);
}
while ((c = fgetc(fp1)) != EOF)
    fputc(c, fp3);
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;
}

Output:

Merged file1.txt and file2.txt into file3.txt

file.txt


 





file2.txt







merge.txt



No comments:

Post a Comment