The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Number of line in a text file
Replies: 12   Last Post: Jan 23, 2013 1:51 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 138
Registered: 1/30/06
Re: Number of line in a text file
Posted: Nov 10, 2006 5:37 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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

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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.