Right Click to Search

Monday, October 18, 2024

C Program to Copy Text From One File to Other File




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
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


Tags / Keywords : | , ,

Stumble
Delicious
Technorati
Twitter
Facebook

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

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates