note 54776 added to function.fgets
| From: | intox at cariboucrew dot com | Date: | Thu, 14 Jul 2005 18:59:58 +0000 |
| Subject: | note 54776 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91983@lists.php.net to get a copy of this message | ||
In response to Allen:
"The original example could be coded as:
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
if (!feof($handle)) {
echo $buffer;
}
}
fclose($handle);
?>"
This is fine except that this code checks for EOF twice. If there is no newline at the end of a file
the very last line will not be parsed even if there is data on that line. This is better yet:
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
}
fclose($handle);
?>
IntoX
----
Manual Page -- http://www.php.net/manual/en/function.fgets.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54776
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54776&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54776&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54776&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54776&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54776&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54776&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54776&report=yes
Search -- http://master.php.net/manage/user-notes.php