Login to ask a question from Data Files

Write a C Program to merge contents of two files into a third file.

#include  

#include  

void main() 

{ 

   // Open two files to be merged 

   FILE *fp1 = fopen("file1.txt", "r"); 

   FILE *fp2 = fopen("file2.txt", "r"); 

   // Open file to store the result 

   FILE *fp3 = fopen("file3.txt", "w"); 

   char c; 

   if (fp1 == NULL || fp2 == NULL || fp3 == NULL) 

   { 

         puts("Could not open files"); 

         exit(0); 

   } 

   // Copy contents of first file to file3.txt 

   while ((c = fgetc(fp1)) != EOF) 

    {

      fputc(c, fp3); 

     }

   // Copy contents of second file to file3.txt 

   while ((c = fgetc(fp2)) != EOF) 

    {

      fputc(c, fp3); 

    }

   printf("Mer....

Show More
Previous Question Next Question
🔗

Similar Questions

From Data Files · C programming

View All
WhatsApp