Reading a specific line in a file6th April 2008, 669 views After reading something on the php-general list I decided that a) I'm bored, and b) I'll write something which handles it. So here it is. It's very simple when it comes down to it, but does incorporate a line cache so that if you're constantly jumping around large files reading the same line then this should help speed things up slightly. Top 10 referrering pages
Link to meIf you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks.
Author: Richard Heyes
Posted: 6th April 2008 19:33 Brian Holub:
Quote> Just curious if there was a performance reason > why you chose the following: > > ... Not really - I probably just wrote the isCached method after I wrote that bit.
Author: Greg
Posted: 7th April 2008 00:59 Or, SPLFileObject?
Quote$file = new SPLFileObject($filename); $file->seek(12); $line = $file->getCurrentLine();
Author: Hasin
Posted: 7th April 2008 19:35 Greg, you took my bread away, heh heh. I just wrote almost the same code using SplFileObject, came to post as a comment and saw that you've already done it. Here is what I wrote
Quote$linereader= new SplFileObject("/path/to/your/file"); $linenumber = 16; $linereader->seek($linenumber); $line = $linereader->current(); echo $line;
Author: Richard Quadling
Posted: 8th April 2008 16:23 If you have said not to cache, the cache is still updated.
QuoteI suppose if you are wasting cpu cycles NOT caching, then by storing the data in the cache (and never using it) is in the same vein. $this->cache[$number] = $line; should be ... if ($this->useCache) { $this->cache[$number] = $line; } But only if you care enough. Regards, Richard "Not Richard" Quadling. |

Comments
Posted: 6th April 2008 18:53
/**
* Check the line cache first
*/
if ($this->useCache AND isset($this->cache[$number])) {
return $this->cache[$number];
}
instead of using the $this->isCached($number)?
Thanks,
Brian