#28371 [Opn]: Different behaviour of fopen depending on r+,w+,a+
| From: | rc at opelgt dot org | Date: | Thu, 13 May 2004 12:28:22 +0000 |
| Subject: | #28371 [Opn]: Different behaviour of fopen depending on r+,w+,a+ | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-59577@lists.php.net to get a copy of this message | ||
ID: 28371
User updated by: rc at opelgt dot org
Reported By: rc at opelgt dot org
Status: Open
Bug Type: Filesystem function related
Operating System: MacOSX 10.3
PHP Version: 4.3.4
New Comment:
I think it could be mention in the manual:
In PHP 4.2.3 the read pointer for fopen with 'a' is at
the end of file. Instead in 4.3.4 the pointer is at '0'!
So rewind for reading in 4.3.4 with 'a+' is no longer
nessessary.
Rüdiger
Previous Comments:
------------------------------------------------------------------------
[2004-05-12 19:12:34] rc at opelgt dot org
Do you really think when such strange differences occur
and the fopen description says nothing about it, its a
question for support?
Do you know why this differences occurs?
Output with w+ is:
Inhalt<BR>
alt: <BR>
neu:
and with r+:
Inhalt<BR>
alt: 1234567890<BR>
neu:
Rüdiger
------------------------------------------------------------------------
[2004-05-12 18:52:46] wez@php.net
The output under both linux and OSX is:
Inhalt<BR>
alt: 1234567890<BR>
neu: 13579
Your bug report doesn't sound so much like a bug report, but a support
request; this isn't a support forum, so if you don't understand these
results even after careful scrutiny of the fopen() manual page, please
visit http://www.php.net/support.php to find someone
who might be able
to help you.
------------------------------------------------------------------------
[2004-05-12 17:20:59] rc at opelgt dot org
Description:
------------
Hello,
for my skill its a bit too much to understand whats
going on here.
Reproduce code:
---------------
<?php
$alt = '1234567890';
$neu = '13579';
$datei = 'test.txt';
$dh = fopen ($datei,"w");
fwrite($dh, $alt);
fclose($dh);
// pos1
$dh = fopen ($datei,"a+"); // just alter the mode to test others
rewind($dh);
echo "Inhalt<BR>\nalt: ".fread($dh, filesize($datei))."<BR>\n";
ftruncate($dh, '0');
fwrite($dh, $neu);
rewind($dh);
echo 'neu: '.fread($dh, filesize($datei));
fclose($dh);
?>
Expected result:
----------------
I wrote the code above and exspected same behaviour for
r+, w+ and a+.
Before pos1 The content of 'test.txt' is written to
'1234567890'.
After pos1 the file is opened anothertime with r+,w+ or
a+.
Rewind sets the pointer to '0'.
So that fread can read the file from the beginning.
Ftruncate makes the size of the file to '0' and sets the
pointer to the beginning of the file.
Fwrite writes the new content and the size of the file
will be as big as nessessary for the content.
Rewind takes the pointer back from end to beginning of
the file.
Actual result:
--------------
Echo should tell the new content.
This only works with a+, but with r+ and w+ nothing is
displayed.
The first echo command tells the 'old' content in a+ and
r+ mode, but not with w+.
Seems to me there are undocumented differences between
the modes that makes it difficult to choose and use one
of them.
Rüdiger
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28371&edit=1