With C, is there a way to read only the last line of a file without looping it over all the content?
The thing is that the file contains millions strings, each of which contains an integer (long long int). The file itself can be quite large, I suppose, even up to 1000mb. I know for sure that the last line will not be longer than 55 digits, but there may be 2 digits. Of the options to use any database ... I have already considered this.
This may be a dumb question, but based on PHP, it's hard for me to answer. I searched everywhere but found nothing.
I am currently using:
if ((fd = fopen(filename, "r")) != NULL) // open file { fseek(fd, 0, SEEK_SET); // make sure start from 0 while(!feof(fd)) { memset(buff, 0x00, buff_len); // clean buffer fscanf(fd, "%[^\n]\n", buff); // read file *prefer using fscanf } printf("Last Line :: %d\n", atoi(buff)); // for testing I'm using small integers }
Thus, I loop the contents of the file, and as soon as the file becomes larger than ~ 500 thousand lines, the situation gets much worse ...
Thanks in advance. Maksim
source share