Develop a C Program to merge the content of file and display the merged content on screen
Code
#include <stdio.h>#include <stdlib.h>int main(){// Open two files to be mergedFILE *fp1 = fopen("file1.txt", "r");FILE *fp2 = fopen("file2.txt", "r");// Open file to store the resultFILE *fp3 = fopen("file3.txt", "w");char c, ch;if (fp1 == NULL || fp2 == NULL || fp3 == NULL){puts("Could not open files");exit(0);}// Copy contents of first file to file3.txtwhile ((c = fgetc(fp1)) != EOF)fputc(c, fp3);// Copy contents of second file to file3.txtwhile ((c = fgetc(fp2)) != EOF)fputc(c, fp3);printf("Merged file1.txt and file2.txt into file3.txt and content is\n\n");fclose(fp1);fclose(fp2);fclose(fp3);fp3 = fopen("file3.txt", "r");if (fp3 == NULL){perror("Error while opening the file.\n");exit(EXIT_FAILURE);}while ((ch = fgetc(fp3)) != EOF)printf("%c", ch);printf("\n\n");fclose(fp3);return 0;}
Output
Yo
0 Comments
Post a Comment