|
|
Re: Number of line in a text file
Posted:
Nov 10, 2006 5:37 AM
|
|
Benoît Valley wrote: > Ok. I have now a very efficient way to know the number of line in my > text file. Is it now possible to read very quickly just the last line > of the file ? > ...
If you know the maximum length, MAXLEN, of a line it's rather straightforward:
open the file use fseek to reposition the file position indicator MAXLEN+1 chars from the end read to end of file in one chunk find the chars between the penultimate and last newline
If you don't know MAXLEN, you have to choose some appropriate MAXLEN, read a chunk and, if there is no newline in the chunk, reposition with fseek and read in the previous chunk.
In this case you also have to deal with the fact that the line you are looking for can span more than one chunk.
jewa & wagberg , net
|
|