Resources » Articles/Knowledge Sharing » Computer & Technology

C program: To Copy Contents From One File To Another


Posted Date: 06-Nov-2010  Last Updated:   Category: Computer & Technology    
Author: Member Level: Bronze    Points: 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

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.