470,849 Members | 1,135 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Skip lines in txt-file

I have to process large files containing "logical" blocks of strings, each
block varying in number of lines.
....
Block number x
string1
string2
....
string n
Block number y
et cetera.

Blocks are read and processed one by one. My GetNextBlock ($index) function
returns an array with all lines in a block, and returns the line number of
the next block header.
What is the fastest way to skip those first <$index -1> lines the next read
? I cannot guarantee (unfortunately) the file is still open, so the file
pointer must be assumed at start of file again.
I had an intermittent solution in which I first split all the blocks into
separate files, but that caused a lot of other unwanted effects beyond the
scope of this posting.

Hints, pointers appreciated!
TIA
Pjotr
Jul 17 '05 #1
2 2786
"Pjotr Wedersteers" <x3****@westerterp.com> wrote in message news:<41***********************@news.xs4all.nl>...
I cannot guarantee (unfortunately) the file is still open, so the file


Why not?
If you open it, it's going to remain open until you close it...

Just how big is this file?
Could you not process all blocks on initial opening?

If it is closed between reads (not processing all at once):
is the file being modified when closed?
you could perhaps keep track of what position in file you're at.
Or on the 1st pass through... locate starting position of all blocks...
Jul 17 '05 #2
"Pjotr Wedersteers" <x3****@westerterp.com> wrote in message
news:<41***********************@news.xs4all.nl>...

What is the fastest way to skip those first <$index -1> lines
the next read ?


Read the whole file into an array via file() and access the desired
line by number. The array could then be serialized and written into
a session variable to get rid of additional reads of the file.

Cheers,
NC
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Ramon Felciano | last post: by
3 posts views Thread by rasdj | last post: by
8 posts views Thread by Xah Lee | last post: by
3 posts views Thread by Matt Williamson | last post: by
23 posts views Thread by Paul Mars | last post: by
9 posts views Thread by paktsardines | last post: by
3 posts views Thread by jackson.rayne | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.