Resources » Articles/Knowledge Sharing » Computer & Technology

C program: To Copy Contents From One File To Another


Updated: Category: Computer & Technology
Author: Member Level: BronzePoints: 2







#include

void main()
{
FILE *fp1,*fp2;
char c;

//Creating file1.txt
fp1=fopen("file1.txt","w");
printf("Enter data for file1.txt:\n");
if(fp1!=NULL)
{
c=getchar();
while(c!='\n')
{
putc(c,fp1);
c=getchar();
}
fclose(fp1);
}

printf("\nThe data for file1.txt is entered.\n");

//Copying contents from file1.txt to file2.txt
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
c=getc(fp1);
while(c!=EOF)
{
putc(c,fp2);
c=getc(fp1);
}
fclose(fp1);
fclose(fp2);


//Printing the contents of file2.txt
fp2=fopen("file2.txt","r");
printf("\nAfter copying, the contents in file2.txt:\n");
c=getc(fp2);
while(c!=EOF)
{
printf("%c",c);
c=getc(fp2);
}
fclose(fp2);

printf("\n\n");
}


Did you like this resource? Share it with your friends and show your love!




Responses to "C program: To Copy Contents From One File To Another"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index
    Complete the action items below to enter to win an iPad Mini from India Study Channel! in India Study Channel - Locker on LockerDome
    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    Online Members

    Naveen
    More...
     
    ISC Technologies, Kochi - India. © All Rights Reserved.