C program to copy contents of one file to another
#include<stdio.h> #include<conio.h> void main() { char sk, source[15], target[15]; FILE *source_ptr, *target_ptr; clrscr(); printf("Enter the source file name: "); scanf("%s",source); source_ptr=fopen(source,"r"); printf("Enter the target file name: "); scanf("%s",target); target_ptr=fopen(target,"w+"); if(source_ptr==NULL) { printf("No content!"); printf("\n press any key.."); getch(); exit(0); } while(!feof((source_ptr)) { sk=getc(source_ptr)); putc(sk,target_ptr); } printf("The contents of the target file are: "); printf("\n _ _ _ _ _ _ _ _\n"); rewind(target_ptr); while(!feof((target_ptr)) { sk=getc(target_ptr)); printf("%c",sk); } fclose(source_ptr); fclose(target_ptr); printf("\n Press any key ..."); getch(); }
Output:
Enter the source file name: SAMPLE.TXT
Enter the target file name: SAMPLE.BAK
The contents of the target file are:
_ _ _ _ _ _ _ _
C Programming is one of the most powerful programming languages,
C Programming was developed by Dennis Ritchie at Bell Laboratories in 1970.
Press any key …