Learn C ProgrammingLearn ProgrammingPrograms On File OperationsResources

100. Merge two files by store data into third one

By September 18, 2012 September 5th, 2018 No Comments
#include<stdio.h>
#include<stdlib.h>

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

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

   FILE *fp3 = fopen("file3.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);

   fclose(fp1);
   fclose(fp2);
   fclose(fp3);
   return 0;
}
DEMO01