php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28926 feof() problems if 2 files are open
Submitted: 2004-06-25 17:33 UTC Modified: 2005-01-24 00:56 UTC
From: jochen at keutel dot de Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.3.7 OS: Solaris 8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jochen at keutel dot de
New email:
PHP Version: OS:

 

 [2004-06-25 17:33 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-24 00:56 UTC] hholzgra@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 07:01:30 2024 UTC