php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35495 feof() does not report EOF on a zero file
Submitted: 2005-11-30 18:30 UTC Modified: 2005-12-01 02:34 UTC
From: dsp at tdcspace dot dk Assigned: wez (profile)
Status: Not a bug Package: Filesystem function related
PHP Version: 5CVS, 4CVS (2005-11-30) (snap) OS: linux/win
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: dsp at tdcspace dot dk
New email:
PHP Version: OS:

 

 [2005-11-30 18:30 UTC] dsp at tdcspace dot dk
Description:
------------
feof() does not report eof on a zero (0) length file

following code (similar to the ex. in the php manual) does
act like a new record was read - allthouh the file IS at eof.

$handle = @fopen("xxx", "r");
while (!feof($handle)) {
   $buffer = fgets($handle, 4096);
   echo $buffer;
   }
fclose($handle);



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-30 18:33 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.1-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.1-win32-latest.zip


 [2005-11-30 18:42 UTC] dsp at tdcspace dot dk
is there a cvs for 4.4.1
 [2005-11-30 18:42 UTC] sniper@php.net
No, try the 5.1 snapshot.
 [2005-11-30 22:40 UTC] dsp at tdcspace dot dk
nope - 5.1.2.2 cvs - same story - feof() still ignores a zero file
 [2005-11-30 22:46 UTC] dsp at tdcspace dot dk
what i don't understand is that the win/linux lower filesystem layer maintains a filepointer of which it should be easy to detect/report a EOF condition. The PHP handle as a file descriptor resource should among things maintain this.

But i don't maintain php and thus don't know the reasons.
 [2005-11-30 23:43 UTC] sniper@php.net
Wez, sounds like something isn't working quite well in the streams world?
 [2005-12-01 01:07 UTC] tony2001@php.net
I get the same result with this C code:

#include <stdlib.h>
#include <stdio.h>

int main() {
    FILE *fd;
    fd = fopen("/tmp/xxx", "r");
    while (!feof(fd)) {
        char buf[100];
        fgets(buf, 100, fd); // remove that and you'll get endless cycle
        printf("read\n");
    }
    fclose(fd);
    return 0;
}
(it reads once and stops just after that)
Doesn't look like a bug to me.
PHP behaves in the same way as the underlying OS.
 [2005-12-01 02:34 UTC] dsp at tdcspace dot dk
Taken from the PHP manual
--------------------------------
feof -- Tests for end-of-file on a file pointer

Description

bool feof ( resource handle )

Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE. 
--------------------------------

Read a file with only 4 chars "0123". The chars correspond to what ftell() reports (0 at open) and 4 after reading 4 with fgets(5).

File-pointer is then 4 - aka the next read position which is beyond last char and thus EOF. This must be proof enough of inconsistency. Either the manual or the feof() need to be corrected - i prefer feof().

Still php is great - and i help to make it better !
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC