#40183 [Bgs]: fopen() for appending does not move position until first I/O
| From: | perrog at gmail dot com | Date: | Mon, 22 Jan 2007 08:59:53 +0000 |
| Subject: | #40183 [Bgs]: fopen() for appending does not move position until first I/O | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-107862@lists.php.net to get a copy of this message | ||
ID: 40183
User updated by: perrog at gmail dot com
Reported By: perrog at gmail dot com
Status: Bogus
Bug Type: Filesystem function related
Operating System: Mac OS X 10.4
PHP Version: 5.2.1RC3
New Comment:
Yes, you have right. I became aware that even the ISO C
Library Standard mentions ftell() immediately after fopen()
returns a value that is implementation-defined (17.19.3):
"If a file can support positioning requests (such as a disk
file, as opposed to a terminal), then a file position
indicator associated with the stream is positioned at the
start (character number zero) of the file, unless the file
is opened with append mode in which case it is
implementation-defined whether the file position indicator
is initially positioned at the beginning or the end of the
file."
Previous Comments:
------------------------------------------------------------------------
[2007-01-21 19:01:13] iliaa@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
The initial position returned is 0 because counting starts
from the append position (earlier data does not count).
------------------------------------------------------------------------
[2007-01-21 07:26:48] perrog at gmail dot com
Description:
------------
Opening a file for appending does not move the file pointer to
the end of file before the first I/O operation is performed.
That makes ftell() return zero directly after the fopen()
call.
The workaround, if you need to call ftell() immediately after
the fopen(), is to use a no-operation I/O call fseek() that
don't do anything. Thus, the file pointer is updated and
returns correct position information.
Reproduce code:
---------------
// create or open a file, and write some stuff to it.
$filename = "/path/to/file.txt";
$fp = fopen($filename, "a+");
fwrite($fp, "Sample text");
fclose($fp);
// re-open it and check what ftell() returns
$fp = fopen($filename, "a+");
echo " pre-fseek: " . ftell($fp) . "\n"; // ftell returns 0
fseek($fp, 0, SEEK_END);
echo "post-fseek: " . ftell($fp) . "\n"; // ftell now ok
Expected result:
----------------
The snippet should print the file size of the file:
pre-fseek: 11
post-fseek: 11
Actual result:
--------------
The snippet should print the correct file size only after the
first I/O operation:
pre-fseek: 0
post-fseek: 11
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40183&edit=1