note 67261 added to function.feof
| From: | ironoxid at libero dot it | Date: | Tue, 06 Jun 2006 13:52:37 +0000 |
| Subject: | note 67261 added to function.feof | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-113359@lists.php.net to get a copy of this message | ||
I really thought that the feof() was TRUE when the logical file pointer is a EOF.
but no !
we need to read and get an empty record before the eof() reports TRUE.
So
$fp = fopen('test.bin','rb');
while(!feof($fp)) {
$c = fgetc($fp);
// ... do something with $c
echo ftell($fp), ",";
}
echo 'EOF!';
prints for two time the last byte position.
If our file length is 5 byte this code prints
0,1,2,3,4,5,5,EOF!
Because of this, you have to do another check to verify if fgetc really reads another byte (to
prevent error on "do something with $c" ^_^).
To prevent errors you have to use this code
$fp = fopen('test.bin','rb');
while(!feof($fp)) {
$c = fgetc($fp);
if($c === false) break;
// ... do something with $c
}
but this is the same of
$fp = fopen('test.bin','rb');
while(($c = fgetc($fp))!==false) {
// ... do something with $c
}
Consequently feof() is simply useless.
Before write this note I want to submit this as a php bug but one php developer said that this does
not imply a bug in PHP itself (http://bugs.php.net/bug.php?id=35136&edit=2).
If this is not a bug I think that this need at least to be noticed.
Sorry for my bad english.
Bye ;)
----
Server IP: 213.92.11.19
Probable Submitter: 151.56.79.171
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.feof.php
Edit -- http://master.php.net/note/edit/67261
Del: integrated -- http://master.php.net/note/delete/67261/integrated
Del: useless -- http://master.php.net/note/delete/67261/useless
Del: bad code -- http://master.php.net/note/delete/67261/bad+code
Del: spam -- http://master.php.net/note/delete/67261/spam
Del: non-english -- http://master.php.net/note/delete/67261/non-english
Del: in docs -- http://master.php.net/note/delete/67261/in+docs
Del: other reasons-- http://master.php.net/note/delete/67261
Reject -- http://master.php.net/note/reject/67261
Search -- http://master.php.net/manage/user-notes.php