Re: How to jump to line number in large file
| From: | Steve McGill | Date: | Sat, 05 Apr 2008 08:43:04 +0000 |
| Subject: | Re: How to jump to line number in large file | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-272563@lists.php.net to get a copy of this message | ||
Thanks for the heads up on fgetc() incrementing by one. I hadn't actually
tested that code yet, I was using the original fseek($handle,$pos).
strpos would be ideal but it needs to work on a string and not a file - I
don't want to load a 100Mb file into memory if I don't have to. Perhaps I
should test how quick the fgets() and ftell() method is because at least it
loads in one line at a time.
Does anybody know any other ways to go about the problem?
Many thanks,
Steve
""Greg Bowser"" <topnotcher@gmail.com> wrote in message
news:9ed554210804041656m1533cd2dkf9bdeb0116a3d563@mail.gmail.com...
> fseek($handle, 1, SEEK_CUR); // or fseek($handle, $pos)
> $t = fgetc($handle);
>
> This probably won't help you, but given a quick glance, it looks like
> you're
> incrementing the file pointer by 2 positions on each iteration of your
> while
> loop. The fgetc() function returns the character at the current position
> and
> increments the file pointer by 1.
>
> I haven't tried this, but perhaps using strpos would be faster? Use strpos
> to seek to find the first "\n", then use the offset parameter to seek to
> the
> second, and so on, until strpos() returns false.
>
> --GREG
>