I need to write out .txt file backwards in terminal using recursion, but it seems I'm stuck. This is my code so far, but it creates an infinite loop. Also, the procedure write() should have only 1 parameter - pointer to file.
#include <stdio.h>
void write(FILE **f)
{
char cur;
fseek(*f,-1,SEEK_CUR);
cur = fgetc(*f);
printf("%c",cur);
write(f);
}
int main(void)
{
FILE *f;
f = fopen("text.txt","r");
fseek(f, 0, SEEK_END);
write(&f);
fclose(f);
return 0;
}
I wanted to use fseek and ftell functions, they seem to be best way to do this.
My expected output would be:
FILE
Hello world
OUTPUT
dlrow olleH