C Program to Append the Content of One File to the End of Another File

«
»
This C Program appends the content of file at the end of another.

Here is source code of the C Program to append the content of file at the end of another. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Append the Content of File at the end of Another
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. main()
  8. {
  9.     FILE *fsring1, *fsring2, *ftemp;
  10.     char ch, file1[20], file2[20], file3[20];
  11.  
  12.     printf("Enter name of first file ");
  13.     gets(file1);
  14.     printf("Enter name of second file ");
  15.     gets(file2);
  16.     printf("Enter name to store merged file ");
  17.     gets(file3);
  18.     fsring1 = fopen(file1, "r");
  19.     fsring2 = fopen(file2, "r");
  20.     if (fsring1 == NULL || fsring2 == NULL)
  21.     {
  22.         perror("Error has occured");
  23.         printf("Press any key to exit...\n");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.     ftemp = fopen(file3, "w");
  27.     if (ftemp == NULL)
  28.     {
  29.         perror("Error has occures");
  30.         printf("Press any key to exit...\n");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     while ((ch = fgetc(fsring1)) != EOF)
  34.         fputc(ch, ftemp);
  35.     while ((ch = fgetc(fsring2) ) != EOF)
  36.         fputc(ch, ftemp);
  37.     printf("Two files merged  %s successfully.\n", file3);
  38.     fclose(fsring1);
  39.     fclose(fsring2);
  40.     fclose(ftemp);
  41.     return 0;
  42. }

$ cc pgm47.c
$ a.out
Enter name of first file a.txt
Enter name of second file b.txt
Enter name to store merged file merge.txt
Two files merged merge.txt successfully.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on File Handling, go to File Handling. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.