#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
fp1 = fopen("Sample.txt","r");
Step 2 : Open Target File in Write Modefp2 = fopen("Output.txt","w");
Step 3 : Read Source File Character by Characterwhile(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



0 Comments:
Post a Comment
Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email