Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. // Open two files to be merged
  7. FILE *fp1 = fopen("file1.txt", "r");
  8. FILE *fp2 = fopen("file2.txt", "r");
  9.  
  10. // Open file to store the result
  11. FILE *fp3 = fopen("file3.txt", "w");
  12. char c;
  13.  
  14. if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
  15. {
  16. puts("Could not open files");
  17. exit(0);
  18. }
  19.  
  20. // Copy contents of first file to file3.txt
  21. while ((c = fgetc(fp1)) != EOF)
  22. fputc(c, fp3);
  23.  
  24. // Copy contents of second file to file3.txt
  25. while ((c = fgetc(fp2)) != EOF)
  26. fputc(c, fp3);
  27.  
  28. printf("Merged file1.txt and file2.txt into file3.txt");
  29.  
  30. fclose(fp1);
  31. fclose(fp2);
  32. fclose(fp3);
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement