\* C file handling program to Reverse the Contents of a File and Print it on output screen *\
# include < stdio.h >
# include < errno.h >
# include < string.h >
# include < stdlib.h >
long count_characters( FILE * )
int main( )
{
int i ;
long cnt ;
char ch, ch1, filename[20], revfilename[20] ;
FILE *fp1, *fp2 ;
printf("\n Enter the filename to be opened : ") ;
scanf("%s", filename) ;
printf("\n Enter the filename to copied the reverse order content : ") ;
scanf("%s", revfilename ) ;
fp1 = fopen(filename, "r") ;
if (fp1 != NULL)
{
printf("\n The FILE has been opened !") ;
fp2 = fopen(revfilename, "w") ;
cnt = count_characters( fp1 ) ;
fseek(fp1, -1L, 2) ;
printf("\n Number of characters to be copied %d", ftell(fp1)) ;
while (cnt)
{
ch = fgetc(fp1) ;
fputc(ch, fp2) ;
fseek(fp1, -2L, 1) ;
cnt-- ;
}
printf("\n **File copied successfully in reverse order** \n") ;
}
else
{
printf("\n The file is not open !\n") ;
}
printf("\n Content in New generated file are : \n") ;
fclose( fp2 ) ;
fp2 = fopen(revfilename, "r") ;
ch = fgetc( fp2 ) ;
while ( ch != EOF )
{
printf ("%c", ch ) ;
ch = fgetc(fp2) ;
}
fclose(fp1) ;
fclose(fp2) ;
return 0 ;
}
long count_characters( FILE * F)
{
fseek(f, -1L, 2) ;
long last_pos = ftell(f) ;
last_pos++ ;
return last_pos ;
}