Hello,
I have code where it is appropriate for me to jump to certain lines in a
text file and edit that particular line. I need to keep a record of the byte
position of every line so that I can fseek to it.
I need to do in-line editing so I don't want to write the entire file out
again etc. I don't want to read the entire file into memory using file().
Finally each line has an unpredicatable line length as it is generated by a
3rd party program so I can't do some maths to work out the correct byte
position.
I am using code like this to keep a record of all the byte positions:
$line_positions = array(1=>0);
$line_number = 2;
while(!feof($handle)) {
fseek($handle, 1, SEEK_CUR); // or fseek($handle, $pos)
$t = fgetc($handle);
if($t == "\n") {
$line_positions[$line_number] = $pos + 1;
$line_number++;
}
$pos++;
}
This behaves fine with smaller textfiles but not very well with 100Mb files,
etc.
I've commented out assigning the array in the loop and that doesn't really
help, it seems it's the fseek fgetc etc which takes forever.
Can anybody point me in the right direction? Editplus opens up the 100Mb
file in a couple of seconds and can then jump to a line number
instantaneously, whereas my code takes a couple of minutes to loop through
the file. I'm assuming this isn't the difference between PHP & C but rather
that I'm doing something stupid :-)
Many thanks in advance for your time,
Steve
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php