|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-07-18 16:48 UTC] kraghuba at in dot ibm dot com
Description:
------------
fgetc() sets the end of the file flag to ture when reading from a file which opened in write only mode.
This failure is applicable on php5 and php6
php version:
PHP 6.0.0-dev (cli) (built: Jul 18 2007 20:53:03) (GCOV)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2007 Zend Technologies
&
PHP 5.2.4-dev (cli) (built: Jul 18 2007 20:49:53) (GCOV)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
OS : RHEL 5
configure setup:
./configure --enable-gcov
Reproduce code:
---------------
<?php
$fp = fopen("test.txt", "w");
var_dump( fwrite($fp, "Text") );
// rewind the file pointer to begining
var_dump( rewind($fp) );
var_dump( ftell($fp) );
var_dump( feof($fp) );
// try to read
var_dump( fgetc($fp) );
var_dump( ftell($fp) );
var_dump( feof($fp) );
fclose($fp);
unlink("test.txt");
?>
Expected result:
----------------
output - php5:
--------------
int(4)
bool(true)
int(0)
bool(false)
bool(false)
int(0)
bool(false)
output - php6:
--------------
int(4)
bool(true)
int(0)
bool(false)
bool(false)
int(0)
bool(false)
Actual result:
--------------
output - php5:
--------------
int(4)
bool(true)
int(0)
bool(false)
bool(false)
int(0)
bool(true)
output - php6:
--------------
int(4)
bool(true)
int(0)
bool(false)
string(1) ""
int(0)
bool(true)
output on php6 when run using run-test.php :
int(4)
bool(true)
int(0)
bool(false)
string(1) "?"
int(0)
bool(true)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 09:00:01 2025 UTC |
I have tried similar stuff on C language and and found that feof doesn't get changed by using fgetc() on file opened in write only mode. The documentation of fgetc() for C says that it returns an int or EOF on end of the file or an *error*. I haven't have enough time to look at any other version other than php6 and 5 (: I think that same thing can be done for php, the application level error should be reported as false and this should be documented. Even other wise if you decide to have the EOF as application level error then it should be documented. Sample code that i use for checking it fgetc() on C : #include <stdio.h> int main ( void ) { FILE *fp; char ch; fp = fopen("test.txt", "w"); fwrite("testing", 7, 1, fp); rewind(fp); printf("\nftell() returns = %d\n", ftell(fp) ); printf("\nfeof() returns = %s\n", ( feof(fp) ? "true" : "false") ); // try read printf("\nfgets() returns = %d\n", fgetc(fp) ); printf("\nftell() returns = %d\n", ftell(fp) ); printf("\nfeof() returns = %s\n", ( feof(fp) ? "true" : "false") ); fclose(fp); } Output: ftell() returns = 0 feof() returns = false fgets() returns =-1 ftell() returns = 0 feof() returns = false