note 8909 deleted from function.fgets by sniper
| From: | sniper@php.net | Date: | Sat, 08 Feb 2003 20:08:34 +0000 |
| Subject: | note 8909 deleted from function.fgets by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-43655@lists.php.net to get a copy of this message | ||
The previous post did not clearly address the real problem. fgets will automatically terminate
reading at the end of the line, which is correct behaviour. The problem comes when one individual
line is longer than your buffer. No matter what size your buffer, this can happen.<br>
There is a solution. You can take the length of the string you get back to find out why fgets
stopped reading. If you came to the end of a line, then strlen($string) will be less than BUFSIZ-1.
There is a case where the line is exactly the length of your buffer, so check if $string[BUFSIZ-2]
== "\n" to determine if that is the case.<br>
The remaining case is that the line is longer than your buffer. What you do next depends on your
intention. If you are looking for a string, then you only need the last strlen($needle)-1 characters
of $string prepended to your next fgets.<br>
On the other hand, if you consider a really long line to be bad data, you might throw out everything
you fgets until you pass one of the first two tests.<br>
The point is to think about what you are doing and why. Also, good functional interfaces help a lot.