C program to copy content of one file to another:
Below C program is used to copy the content of one file into another. If you want to add error handling on file operation such as fopen(), fclose(), please refer https://www.fresh2refresh.com/c-programming/c-file-handling/fopen-fclose-gets-fputs-functions-c/ for better understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <stdio.h> int main() { char ch; FILE *fp1; FILE *fp2; /* Assume this test1.c file has some data. For example “Hi, How are you?” */ if (fp1 = fopen("test1.c", "r")) { ch = getc(fp1); // Assume this test2.c file is empty fp2 = fopen("test2.c", "w+") while (ch != EOF) { fputc(ch, fp2); ch = getc(fp1); } fclose(fp1); fclose(fp2); return 0; } return 1; } |