C programming : C program to copy one file to another.

#include<stdio.h>
#include<conio.h>

//NOTE: if using TurboC both file.txt and copy.txt are in C:\TC\BIN

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

fp1=fopen("file.txt","r"); //opens file.txt in read mode
fp2=fopen("copy.txt","w"); //creates and opens copy.txt in write mode

if(fp1==0)
  printf("File does not exist\n");
else
{
  while(!feof(fp1))
  {
    c=getc(fp1); //get every character of file.txt
    putc(c,fp2); //put every character of file.txt in copy.txt for copying
  }
fclose(fp1); //close file pointed by fp1
fclose(fp2); //close file pointed by fp2
}
getch();

}

 

Please follow and like us:

Leave a Comment