Program : Copy Text From One File to Other File
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp1,*fp2; char ch; clrscr(); fp1 = fopen("Sample.txt","r"); fp2 = fopen("Output.txt","w"); while(1) { ch = fgetc(fp1); if(ch==EOF) break; else putc(ch,fp2); } printf("File copied succesfully!"); fclose(fp1); fclose(fp2); }
Explanation : To copy a text from one file to another we have to follow following Steps :
Step 1 : Open Source File in Read Mode
Step 1 : Open Source File in Read Mode
fp1 = fopen("Sample.txt","r");
Step 2 : Open Target File in Write Mode
fp2 = fopen("Output.txt","w");
Step 3 : Read Source File Character by Character
while(1) { ch = fgetc(fp1); if(ch==EOF) break; else putc(ch,fp2); }
- “fgetc” will read character from source file.
- Check whether character is “End Character of File” or not , if yes then Terminate Loop
- “putc” will write Single Character on File Pointed by “fp2″ pointer
Input Text File :
Output Written on File
No comments:
Post a Comment