Copy contents of one file to another
Here, You will get an easy program to copy contents of one file to another in c language using file handling. Here first we will create a file(test1.txt) and write some data into that, then we will execute the program.
After execution, as result we will get a file name as text2.txt with the same written contents as text1.txt.
Program to Copy contents of one file to another in C
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 | #include<stdio.h> #include<conio.h> main() { char ch; FILE *fp,*fq; fp=fopen("test1.txt","w"); printf("\nEnter data in text1.txt(end with '$') : "); while(ch!='$') { scanf("%c",&ch); fputc(ch,fp); } fclose(fp); fp=fopen("test1.txt","r"); ch=' '; printf("\n\n\ntest1.txt\n"); while(ch!='$') { ch=fgetc(fp); printf("%c",ch); } fclose(fp); fp=fopen("test1.txt","r"); fq=fopen("test2.txt","w"); ch=' '; while(ch!='$') { ch=fgetc(fp); fputc(ch,fq); } fclose(fp); fclose(fq); printf("\n\n\ntest2.txt\n"); fp=fopen("test2.txt","r"); ch=' '; while(ch!='$') { ch=fgetc(fp); printf("%c",ch); } fclose(fp); getch(); } |
Output
Check out our other C programming Examples