ID:               28926
 Updated by:       [EMAIL PROTECTED]
 Reported By:      jochen at keutel dot de
-Status:           Open
+Status:           Bogus
 Bug Type:         Filesystem function related
 Operating System: Solaris 8
 PHP Version:      4.3.7
 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

feof() returns true only after reading has gone beyond end of file
fgets() reads up to a linebreak so on file 1 the 2nd fgets() will
consume all characters up to the linebreak ending line 2 but
*not* beyond that so it does not reach the actual end of file yet.
As a consequence feof() does only return true after the third
fgets() on file1 has happened which was not able to retrieve 
any further data from file1.

Your 2nd script shows the same effect, add a counter output 
to the beginning of your loop and you'll see that both scripts
terminate in loop iteration #4

I think you were tricked by the fact that
echo "file1: ".$buffer1;
does not switch to a new line if fgets()
wasn't able to fetch any more data
so with your 2nd script the "over" message
is printed on the same line although it is 
created by the following loop iteration only


Previous Comments:
------------------------------------------------------------------------

[2004-06-25 17:33:02] jochen at keutel dot de

Description:
------------
If I open 2 files with fopen and parse them with fgets then feof
doesn't work correctly. Example:

file1: (2 lines)
line 1
line 2

file2: (4 lines)
line 1
line 2
line 3
line 4

The code provided should return only 2 lines of file1 and should stop
than. but it returns 3 lines of file1.

If I remove the code handling file2 from the code
(http://keutel.de/test_feof2.txt) then all works fine.

Reproduce code:
---------------
see http://keutel.de/test_feof1.txt :

$handle1 = fopen ("file1", "r");
$handle2 = fopen ("file2", "r");
while (TRUE) {
    if (feof($handle1) && feof($handle2)) {
      echo "both over\n";
      break;
    }
    if (feof($handle1) || feof($handle2)) {
      echo "one is over\n";
      break;
    }
    $buffer1 = fgets($handle1, 4096);
    $buffer2 = fgets($handle2, 4096);
    echo "file1: ".$buffer1;
    echo "file2: ".$buffer2;
}

Expected result:
----------------
file1: line 1
file2: line 1
file1: line 2
file2: line 2
one is over


Actual result:
--------------
file1: line 1
file2: line 1
file1: line 2
file2: line 2
file1: file2: line 3
one is over



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=28926&edit=1

Reply via email to