C Programming
Computer Programming

C file handling program to Copy File into Another File



Write C file handling program to Copy File into Another File

\* C file handling program to Copy File into Another File *\

# include < stdio.h >
# include < stdlib.h >
# include < string.h >
int   main( )
{

FILE *fp1, *fp2 ;
char ch, filename1[20], filename2[20] ;
int pos ;
printf("\n Enter the source filename to copy the content : ") ;
scanf("%s", filename1) ;
fp1 = fopen(filename1, "r") ;
if (fp1 == NULL)
{
      printf(" Cannot open file for read the content ! \n") ;
      exit(0) ;
}
printf("\n Enter the destination filename to copy the content : ") ;
scanf("%s", filename2 ) ;
fp2 = fopen(filename2, "w" ) ;
if (fp2 == NULL)
{
      printf(" Cannot open file for copy content ! \n") ;
      exit(0) ;
}
fseek(fp1, 0L, SEEK_END) ;
pos = ftell(fp1) ;
fseek(fp1, 0L, SEEK_SET) ;
while ( pos-- )
{
      ch = fgetc( fp1 ) ;
      fputc(ch, fp2) ;
}
fclose( fp1 ) ;
fclose( fp2 ) ;
return 0 ;

}

Output of Program :

Output of C file handling program to Copy File into Another File