Copy the content of one file to another - C

Write a C program to copy the contents of one file into another file.

Solution:

#include<stdio.h>
int main()
{
      int ch;
      FILE *fp,*fq;
      fp=fopen("source.txt","r");
      fq=fopen("backup.txt","w");   
      if(fp==NULL||fq==NULL)
            printf("File does not exist..");
      else
            while((ch=fgetc(fp))!=EOF)
            {
                  fputc(ch,fq);
            }
      printf("File copied.....");
      return 0;
}


Output:

copy file

source

backup

Before executing this program, create source.txt file at the same location where this program is located.

After creating source file, execute this program, you will get a message  'File copied..'.
Now, if you access the location where your program & source file are located, you will get backup.txt with same content as in source file.