Code:
flag = fseek(ifp, fpi, SEEK_END);
elfi = ftell(ifp);
Here is an example that illustrates what you mentioned in the first post, it is indeed possible and quite easy:
Code:
#include <stdio.h>
int main(int argc, char ** argv)
{
FILE * fp = NULL;
char buf[BUFSIZ+1];
long pos = 0;
if (argc<2)
return -1;
for (;argc>1;--argc) {
if (!(fp = fopen(argv[argc-1],"r")))
continue;
/* find end position */
fseek(fp,0L,SEEK_END);
pos = ftell(fp);
/* rewind for reading */
rewind(fp);
while (fgets(buf,BUFSIZ,fp) && !ferror(fp)) {
if (ftell(fp) == pos) {
/* here you could put a continue for example, to skip outputting the last line */
printf("############## JUST BEFORE LAST LINE #################\n");
}
printf("%s",buf);
}
fclose(fp);
}
return 0;
}
I honestly don't know how fast/slow it is to seek to end and then rewind, so perhaps the double buffer idea is smarter. Perhaps an exercise would be to implement both methods and do some benchmarking.