Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code

Reading a specific line in a file


6th 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

  1. http://www.phpdeveloper.org/news/9925 (38 referrals)
  2. http://www.phpdeveloper.org/ (17 referrals)
  3. http://phpdeveloper.org/ (15 referrals)
  4. http://www.google.com/reader/view/ (10 referrals)
  5. http://phpdeveloper.org/news/9925 (4 referrals)
  6. http://www.phpugmunich.org/ (4 referrals)
  7. http://www.google.co.uk/search?q=php+read+specifi... (2 referrals)
  8. http://www.phpdeveloper.org/index/3223 (2 referrals)
  9. http://www.phpdeveloper.org/tag/code (2 referrals)
  10. http://www.phpdeveloper.org/tag/file (2 referrals)
- +
Rate this article

Link to me

If 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.

RSS Feed for Comments

Comments

Author: Brian Holub
Posted: 6th April 2008 18:53
Just curious if there was a performance reason why you chose the following:

/**
* 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
Quote
Author: Richard Heyes
Posted: 6th April 2008 19:33
Brian Holub:
> 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.
Quote
Author: Greg
Posted: 7th April 2008 00:59
Or, SPLFileObject?

$file = new SPLFileObject($filename);

$file->seek(12);

$line = $file->getCurrentLine();
Quote
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


$linereader= new SplFileObject("/path/to/your/file");

$linenumber = 16;

$linereader->seek($linenumber);
$line = $linereader->current();
echo $line;
Quote
Author: Richard Quadling
Posted: 8th April 2008 16:23
If you have said not to cache, the cache is still updated.

I 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.
Quote

Post Comment

Your name:
Your email:
(Don't worry, I won't spam you. Also, if you do put your email address in here, you'll get notified of new comments. If you don't, you won't.)
Comments:
  Do not post support type questions please

 
CAPTCHA image If you can't read the CAPTCHA then press the submit button to get another. Your comment will re-appear (as if by magic...).
Captcha image