By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

feof()

P: n/a
Hallo!

The code here outputs '0'. Why?
----------------------------
$filename = "test.dat";
if (!file_exists($filename)) {
touch($filename); // Create blank file
chmod($filename,0666);
}
$fp=fopen($filename,'r');
if ($fp && !feof($fp)) echo ftell($fp); // should output nothing!
fclose($fp);
----------------------------
Wolfgang


Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Mon, 13 Jun 2005 23:18:15 +0200, "Wolfgang Werners-Lucchini"
<ww*@musensturm.de> wrote:
The code here outputs '0'. Why?
----------------------------
$filename = "test.dat";
if (!file_exists($filename)) {
touch($filename); // Create blank file
chmod($filename,0666);
}
$fp=fopen($filename,'r');
if ($fp && !feof($fp)) echo ftell($fp); // should output nothing!
fclose($fp);


What makes you think it should output nothing?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
> >The code here outputs '0'. Why?
----------------------------
$filename = "test.dat";
if (!file_exists($filename)) {
touch($filename); // Create blank file
chmod($filename,0666);
}
$fp=fopen($filename,'r');
if ($fp && !feof($fp)) echo ftell($fp); // should output nothing!
fclose($fp);


What makes you think it should output nothing?

feof() should be 'true'!
if-condition ('true' && 'false') should be 'false'!

Wolfgang
Jul 17 '05 #3

P: n/a
Wolfgang Werners-Lucchini (ww*@musensturm.de) decided we needed to
hear...
Hallo!

The code here outputs '0'. Why?
----------------------------
$filename = "test.dat";
if (!file_exists($filename)) {
touch($filename); // Create blank file
chmod($filename,0666);
}
$fp=fopen($filename,'r');
if ($fp && !feof($fp)) echo ftell($fp); // should output nothing!
fclose($fp);
----------------------------
Wolfgang


Output of '0' is correct. EOF condition gets set on a read, not on
an open, and you don't read from the file.
--
Dave <da**@REMOVEbundook.com>
(Remove REMOVE for email address)
Jul 17 '05 #4

P: n/a
On Tue, 14 Jun 2005 00:45:05 +0200, "Wolfgang Werners-Lucchini"
<ww*@musensturm.de> wrote:
The code here outputs '0'. Why?
----------------------------
$filename = "test.dat";
if (!file_exists($filename)) {
touch($filename); // Create blank file
chmod($filename,0666);
}
$fp=fopen($filename,'r');
if ($fp && !feof($fp)) echo ftell($fp); // should output nothing!
fclose($fp);


What makes you think it should output nothing?

feof() should be 'true'!


Why?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5

P: n/a
Dave wrote:
Wolfgang Werners-Lucchini (ww*@musensturm.de) decided we needed to
hear...
Hallo!

The code here outputs '0'. Why?
----------------------------
$filename = "test.dat";
if (!file_exists($filename)) {
touch($filename); // Create blank file
chmod($filename,0666);
}
$fp=fopen($filename,'r');
if ($fp && !feof($fp)) echo ftell($fp); // should output nothing!
fclose($fp);
----------------------------
Wolfgang

Output of '0' is correct. EOF condition gets set on a read, not on
an open, and you don't read from the file.


For a lengthy discussion on the feof topic, see this thread on devshed:
http://forums.devshed.com/t244304/s.html&highlight=feof
Jul 17 '05 #6

P: n/a
> >> What makes you think it should output nothing?

feof() should be 'true'!


Why?


The manual says:
Returns TRUE if the (file pointer is at EOF(1)) or (an error occurs(2))

It says NOT:
Returns TRUE if an error occurs

or bettert
Returns TRUE if an READerror occurs

For an empty file EOF-position is 0 (EOF-position is n for file with
filesize n)

Wolfgang

Jul 17 '05 #7

P: n/a
Wolfgang Werners-Lucchini wrote:
What makes you think it should output nothing?
feof() should be 'true'!


Why?

The manual says:
Returns TRUE if the (file pointer is at EOF(1)) or (an error occurs(2))

It says NOT:
Returns TRUE if an error occurs

or bettert
Returns TRUE if an READerror occurs

For an empty file EOF-position is 0 (EOF-position is n for file with
filesize n)

Wolfgang


When you first open a file, no check is done to see what position the
file is actually at. EOF is an error condition, if you go down to the
roots of C, which php's functions are based on. EOF is an error flag
that is set when you reach the end of the file. You can't reach
anything without doing some sort of reading operation though. See the
discussion on devshed for a detailed discussion
http://forums.devshed.com/t244304/s.html&highlight=feof
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.