php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19970 feof()/fgets()/fgetcsv() problem
Submitted: 2002-10-18 01:57 UTC Modified: 2002-10-18 11:46 UTC
From: cynic@php.net Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 4.2.2 OS: FreeBSD 4.7-STABLE
Private report: No CVE-ID: None
 [2002-10-18 01:57 UTC] cynic@php.net
am I the only one who thinks that this should not output anything?

<?php
$fd = fopen(__FILE__, 'r');
while (!feof($fd)) fgets($fd, 4096) || var_dump(feof($fd));
fclose($fd);

I get bool(false) from 4.4.0-dev (cli) and 4.2.2 mod_php.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-18 03:45 UTC] michael dot mauch at gmx dot de
I get bool(true) with 4.2.3 and 4.3.0-dev (from yesterday), both mod_php, Linux 2.4.19.

Maybe it's a documentation problem:

the manual only says about the return value of fgets():

  If an error occurs, returns FALSE.

Nothing is said about the return value on EOF (only "People used to the 'C' semantics of fgets()  should note the difference in how EOF is returned."), so I guess fgets() is free to return whatever it likes on EOF. Was this different in previous versions of PHP?
 [2002-10-18 03:49 UTC] wez@php.net
cynic - can you try HEAD or a snapshot from the last day or so?
I fixed an eof related issue recently.
 [2002-10-18 04:55 UTC] cynic@php.net
michael: i meant bool(true) of course, sorry for the confusion.

wez: still no banana

roman@freepuppy ~/tmp 1027:0 > php -v
PHP 4.3.0-dev (cli), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
roman@freepuppy ~/tmp 1028:1 > cat feof1.php 
<?php
$fd = fopen(__FILE__, 'r');
while (!feof($fd)) fgets($fd, 4096) || var_dump(feof($fd));
fclose($fd);
roman@freepuppy ~/tmp 1029:0 > php feof1.php
bool(true)
roman@freepuppy ~/tmp 1030:0 > 

fgets() gets to read past EOF, but it shouldn't. looks like feof() is broken.

roman@freepuppy ~/tmp 1031:0 > fc -lf |grep "cvs -q up" 
 1016  10/18/2002 11:35  cvs -q up
roman@freepuppy ~/tmp 1032:0 > date
Fri Oct 18 11:48:31 CEST 2002

roman@freepuppy ~/tmp 1033:0 > cat ~/install/php4/config.nice.cli        
#! /bin/sh
#
# Created by configure

'./configure' \
'--enable-cli' \
'--enable-inline-optimization' \
'--enable-ftp' \
'--enable-shmop' \
'--enable-sysvsem' \
'--enable-sysvshm' \
'--enable-sockets' \
'--enable-tokenizer' \
'--disable-session' \
'--disable-shared' \
'--with-openssl' \
'--with-zlib' \
'--with-bz2' \
'--with-curl' \
'--with-gettext' \
'--with-iconv' \
'--with-mcrypt=/usr/local' \
'--with-mhash=/usr/local' \
'--with-ncurses' \
'--with-readline' \
'--with-pear' \
'--with-config-file-path=/usr/local/etc' \
'--with-mysql=/usr/local' \
"$@"

I ./configure --disable-all --enable-cli with the same result.


 [2002-10-18 06:59 UTC] michael dot mauch at gmx dot de
Yes, cynic, you're right, it's a bug.

$fd = fopen(__FILE__, 'r');
while (!feof($fd)) 
{
  $s = fgets($fd);
  if($s === false)
    echo "There was an error! And feof() is ",var_dump(feof($fd));
  // echo $s;
}
fclose($fd);

Prints:

There was an error! And feof() is bool(true)
 [2002-10-18 07:15 UTC] wez@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

It now works for me.
Let me know if it is still borked. :-)
 [2002-10-18 11:46 UTC] cynic@php.net
thanks, wez, works for me, too.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 23:01:28 2024 UTC