reading a file in reverse order

hi
I have a question.

I try to print a ascii file in reverse order( bottom-top).
i've tried this code
#include<stdio.h>
#include <stdlib.h>
/*#define MAX_LINE_SIZE 4096*/
void rev_file_lin(FILE *fp)
{
char buf[100];
char *s;
s=fgets(buf,sizeof buf,fp);
if(NULL==s)
{
if(ferror(fp))
{
perror(NULL);
exit(EXIT_FAILURE);
}
else return;
}
rev_file_lin(fp);
fputs(buf,stdout);
}

int main()
{
FILE *fp;
char buf[100];

fp = fopen("/home/test.txt","r");

fread(&buf,sizeof(buf),1,fp);

rev_file_lin(fp);

/*fgets(buf,sizeof(buf),f);*/

fclose(fp);

printf("%s\n",buf);

/*printf("Hello World\n");*/
return 0;
}

it read the file but not in reverse order, can u help me please????
Read the file. Load the data into a vector, deque or some type of an array and use the reverse() function from #include <algorithm>
If you can use C++, it has the seekg ifstream member function
Topic archived. No new replies allowed.