Print file content in reverse order in C.

 

01 #include<stdio.h>
02
03 int main()
04 {
05 FILE *fp;
06 char c;
07 long counter=0,i;
08 long int initpos;
09 long int limpos;
10
11 printf("Content of file in reverse direction : ");
12
13 fp=fopen("c:/myfile.txt","r");
14 rewind(fp);
15 fseek(fp, -1L, 2);
16 counter = ftell(fp);
17 counter++;
18 while (counter)
19 {
20 c = fgetc(fp);
21 putchar(c);
22 fseek(fp, -2L, 1);
23 counter--;
24 }
25 fclose(fp);
26 return 0;
27 }
 
 
OUTPUT : (if file contain text : Hello C Programming)

Content of file in reverse direction : gnimmargorP C olleH

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.