For a given file, can I count number of lines backwards? i.e. starting from EOF, counting the lines till the beginning?
I could fseek to the end of the file. Start form there, keep looking for new line char (indication of a new line) and keep incrementing my line_number count. But what should be my ending condition?
Is there any opposite of EOF? 🙂
My own suggestion: If I am looking for Xth line from end, I could always get it from beginning at this line number : total_lines – X — fair enough?
Motive: I am interested in getting to Xth line form the end of a HUGE (read really huge) file. So, looking for the most optimal solution.
PS: This is not a homework (though I am a lifetime student :p)
The opposite of EOF is pos == 0.
The POSIX tail command does this; for example code, see http://www.koders.com/c/fid8DEE98A42C35A1346FA89C328CC3BF94E25CF377.aspx