Using file functions to reverse the characters in the file

31 views Asked by At

So, using fseek and ftell function I am trying to print the characters one at a time by first pointing to the end of the file using fseek and then decrementing the value of ftell to print the last character and then print the previous ones.

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

int main()
{
    FILE *fp=NULL;
    char i=0,count=0;

    fp= fopen("note.txt","r");
    if(fp==NULL){   printf("File error");
            return -1;}

    fseek(fp,0,SEEK_END);
    count=ftell(fp);

    while(count){

        count--;
       fseek(fp,count,SEEK_END);
        i=fgetc(fp);
        printf("%c",i);


    }
    fclose(fp);
    fp=NULL;
    return 0;
}

My code is printing some strange characters so it isn't working, any suggestion on where I am going wrong in the while loop?

0

There are 0 answers