note 117578 added to function.feof
| From: | lulzury at Yahoo dot com | Date: | Wed, 01 Jul 2015 17:34:46 +0000 |
| Subject: | note 117578 added to function.feof | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203015@lists.php.net to get a copy of this message | ||
From washington dot edu css342:
On unix/linux, every line in a file has an End-Of-Line (EOL) character and the EOF character is
after the last line. On windows, each line has an EOL characters except the last line. So unix/linux
file's last line is
stuff, EOL, EOF
whereas windows file's last line, if the cursor is on the line, is
stuff, EOF
So set up data files on windows to be the same as on unix/linux. This way, you will correctly
determine eof under both unix/linux and windows. In general, you must exit all loops and all
functions immediately when you are attempting to read an item that would be past the eof.
Here is a typical set up that will work correctly. Suppose in a data file, there are multiple lines
of data. In some function is the loop where you are reading and handling this data. This loop will
look similar to the following.
// infinite loop to read and handle a line of data
for (;;) {
$ln = fgets($fp);
if (feof($fp)) break;
// read the rest of the line
// do whatever with data
}
If you dislike infinite loops, you can accomplish this same thing using a while loop by priming the
loop and reading again at the end:
$ln = fgets($fp);
while (!feof($fp)) {
// read the rest of the line
// do whatever with data
$ln = fgets($fp);
}
----
Server IP: 72.52.91.14
Probable Submitter: 173.167.120.17
----
Manual Page -- http://php.net/manual/en/function.feof.php
Edit -- https://master.php.net/note/edit/117578
Del: integrated -- https://master.php.net/note/delete/117578/integrated
Del: useless -- https://master.php.net/note/delete/117578/useless
Del: bad code -- https://master.php.net/note/delete/117578/bad+code
Del: spam -- https://master.php.net/note/delete/117578/spam
Del: non-english -- https://master.php.net/note/delete/117578/non-english
Del: in docs -- https://master.php.net/note/delete/117578/in+docs
Del: other reasons-- https://master.php.net/note/delete/117578
Reject -- https://master.php.net/note/reject/117578
Search -- https://master.php.net/manage/user-notes.php