fork download
  1. // C program to append the contents of
  2. // source file to the destination file
  3. // including header files
  4. #include <stdio.h>
  5.  
  6. // Function that appends the contents
  7. void appendFiles(char source[],
  8. char destination[])
  9. {
  10. // declaring file pointers
  11. FILE *fp1, *fp2;
  12.  
  13. // opening files
  14. fp1 = fopen(source, "a+");
  15. fp2 = fopen(destination, "a+");
  16.  
  17. // If file is not found then return.
  18. if (!fp1 && !fp2) {
  19. printf("Unable to open/"
  20. "detect file(s)\n");
  21. return;
  22. }
  23.  
  24. char buf[100];
  25.  
  26. // explicitly writing "\n"
  27. // to the destination file
  28. // so to enhance readability.
  29. fprintf(fp2, "\n");
  30.  
  31. // writing the contents of
  32. // source file to destination file.
  33. while (!feof(fp1)) {
  34. fgets(buf, sizeof(buf), fp1);
  35. fprintf(fp2, "%s", buf);
  36. }
  37.  
  38. rewind(fp2);
  39.  
  40. // printing contents of
  41. // destination file to stdout.
  42. while (!feof(fp2)) {
  43. fgets(buf, sizeof(buf), fp2);
  44. printf("%s", buf);
  45. }
  46. }
  47.  
  48. // Driver Code
  49. int main()
  50. {
  51. char source[] = "file1.txt",
  52. destination[] = "file2.txt";
  53.  
  54. // calling Function with file names.
  55. appendFiles(source, destination);
  56.  
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Unable to open/detect file(s)