Program to Copy one file to another file


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

main()
{
 FILE *f1,*f2;
 char c;
 clrscr();

 f1=fopen("Sourcefile.extension","r");

 if(f1==NULL)
 {
  printf("Cannot open the Source file");
  getch();
  exit(0);
 }

 f2=fopen("Targetfile.extension","w");

 if(f2==NULL)
 {
  printf("Cannot open the Target file");
  getch();
  exit(1);
 }

 while(1)
 {
  c=fgetc(f1);

  if(c==EOF)
  break;

  printf("%c",c);

  fputc(c,f2);
 }

 fclose(f1);
 fclose(f2);
 getch();
}

/*In the above program if you don't want to overwrite the existing
information in the target file then you may use "a" instead of "w".
This will insert the  new information after the last line of the the  existing information.
*/

0 comments:

Post a Comment