-1

I currently have a file that appends new entries to the current file. I would like to fetch the 5 most recent entries. How can I read the last line first in C? I would like to use the fgets command to read in line by line if that's possible.

Thank you for your help!

Edit: For instance:

Original File:

The cat is fast.
Dogs are cool.
I like pie.

Desired Output:

I like pie.
Dogs are cool.
The cat is fast.
user3647894
  • 559
  • 1
  • 4
  • 28
  • 3
    possible duplicate of [Reading a text file backwards in C](http://stackoverflow.com/questions/14834267/reading-a-text-file-backwards-in-c) – Schwern Mar 15 '15 at 06:09
  • i saw that answer earlier but am looking to read lines backwards (but not the contents) i have updated my question with an example – user3647894 Mar 15 '15 at 06:11
  • Could you point out how their solutions don't work for you? – Schwern Mar 15 '15 at 06:13
  • That solutions reads in every character backwards. For instance, "the cat is fast" would be read as "tsaf si tac eht". – user3647894 Mar 15 '15 at 06:15
  • 3
    Read the question again and try running the code in the answers. It goes by line just like you ask. "*read in text from the file backwards, buffering it into lines - that is 'abc, def, ghi' should read 'ghi, def, abc' in lines.*" – Schwern Mar 15 '15 at 06:16

2 Answers2

1
while(fgets(buffer,sizeof(buffer),fp); //go on scanning lines

//Now `buffer` holds the last line of `fp`
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • Thanks for the quick response! Unfortunately, I am looking to read every line from end to beginning (not just the last line). Any ideas? – user3647894 Mar 15 '15 at 06:09
  • That requires you read through the whole file. This can get very inefficient for large files. – Schwern Mar 15 '15 at 06:09
  • @user3647894 , Read whole file character by character and store the positions of newline characters. After you have scanned the file, just rewind to the recent newline character position and print until the end of the file. Again rewind the file pointer to the second last newline character position and print everything until a newline character etc – Spikatrix Mar 15 '15 at 06:16
0
#include <stdio.h>

#define N 5 //number of recent entry

int main(void){
    long entry[N+1];//+1 for end of file
    int i, index = 0;
    FILE *fp = fopen("entry.txt", "r");
    char line[128];

    for(i=0;i<N+1;++i)
        entry[i] = -1L;//initialize to invalid value

    do{ //path I : store file position
        entry[index++] = ftell(fp);
        if(index == N+1)
            index = 0;
    }while(EOF!=fscanf(fp, "%*[^\n]%*c"));

    if(--index < 0)//one back index
        index += N+1;
    entry[index] = -1L;//for end of file

    for(i = 0; i < N; ++i){//get N entry
        if(--index < 0)
            index += N+1;
        if(entry[index] < 0L)
            break;//when number of entry < N
        fseek(fp, entry[index], SEEK_SET);
        fgets(line, sizeof line, fp);
        fputs(line, stdout);
    }
    fclose(fp);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 1
    With some explanation this would be a better answer. This reads through the whole file, which is very inefficient for large files like log files. It uses a static line buffer which will cut off lines longer than 128 bytes; instead of fgets it could use fread and calculate the number of bytes per line from the entry index. – Schwern Mar 15 '15 at 09:03
  • You pointed out is your most. This is just a sample. I do not know even whether it is applied to large files do not even know the maximum expected size of the line. If you are trying to apply to large files, it will also be possible to skip from larger the expected size of the file size and the line. Or it could also operate so as to use the previous processing results. – BLUEPIXY Mar 15 '15 at 09:16
  • But I think it is also that it is another problem. I do not want to do such efforts before detailed conditions is shown, also I do not intend to. – BLUEPIXY Mar 15 '15 at 09:18
  • That sort explanation of the limits and assumptions of your code would go well in the answer. – Schwern Mar 15 '15 at 20:30