#22124 [Ver->Bgs]: succesive fgets() and fwrite() calls
| From: | iliaa@php.net | Date: | Sat, 08 Feb 2003 20:26:45 +0000 |
| Subject: | #22124 [Ver->Bgs]: succesive fgets() and fwrite() calls | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-32519@lists.php.net to get a copy of this message | ||
ID: 22124
Updated by: iliaa@php.net
Reported By: rath_rat@hotmail.com
-Status: Verified
+Status: Bogus
Bug Type: Filesystem function related
Operating System: Windows XP Pro / UNIX
PHP Version: 4.3.1-dev
New Comment:
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
After reviewing your script I believe that your program is faulty, you
are tryind to do something that should not be done. Hence the 'errors',
which you are seeing.
The 'r+' mode is primary intended for append operations, where you may
need to read the data already in the file. Since on systems such as
Win32 openning a file with 'a' or 'a+' mode will not allow you to read
the data already in the file. Since the start position will be the end
of the opened file rather then the beginning.
If you want to modify the contents of the existing file, then 1st read
it into memory, make the changes you wish to make and then write it
back to disk.
Previous Comments:
------------------------------------------------------------------------
[2003-02-08 14:20:09] rath_rat@hotmail.com
lol! there you go! well i can tell you that it did work properly on a
UNIX server, i've uploaded it to a Tripod server:
http://members.lycos.co.uk/m0nk3y613/test.php
(lol please excuse the
ads). You can see the text file,
http://members.lycos.co.uk/m0nk3y613/temp.txt
. Also note that since it
is working, so it saves the "c #" line each time, and therefore its one
more each time not nessisarily 5 before and 6 after, although i guess
you figured that out :).
------------------------------------------------------------------------
[2003-02-08 14:06:34] sniper@php.net
Does seem to work quite right in Linux either (using PHP 4.3.1-dev from
today)
------------------------------------------------------------------------
[2003-02-08 13:36:42] rath_rat@hotmail.com
Now here is a script that i've written to show my bug: (sorry it's so
long, but this way the output is clear :)
<?
$tempfile="temp.txt";
if(!$fp= fopen($tempfile, "r+")) {
echo "Opening file failed.\n";
} else {
echo "\nReading original file..\n";
$line=fgets($fp, 4096);
echo "reading $tempfile: \"$line\"..\n";
$line2=fgets($fp, 4096);
echo "reading $tempfile: \"$line2\"..\n";
echo "\nWorking with \$line2\n";
$line2=explode(" ", $line2);
$line2[1]++;
$line2=implode(" ", $line2);
rewind($fp);
echo "\nRewinding $fp..\n";
$line = fgets($fp, 4096);
echo "skipping the first line..\n";
fwrite($fp, $line2);
echo "writing $tempfile: \"$line2\"..\n";
rewind($fp);
echo "\nRewinding $fp..\n";
echo "\nOutputing file again..\n";
$line=fgets($fp, 4096);
echo "reading $tempfile: \"$line\"..\n";
$line2=fgets($fp, 4096);
echo "reading $tempfile: \"$line2\"..\n";
fclose($fp);
}
?>
temp.txt would look like this:
a 1
c 5
without a trailing \n after the 5.
Now what should happen is that the 5 should be incremented to 6 and get
written back to the file. Under *nix this works fine, but under a
windows version (sofar all that i've tried) it does not, the file is
unchanged. My home platform is the Zend Development Enviroment (2.6),
which runs PHP 4.2.2 or higher, under Windows XP Professional. As i
mentioned earlier, under my system the file will be unchanged, but if i
upload the same thing to a UNIX server, it will perform as expected and
change "c 5" to "c 6".
If there's no fgets() call before the fwrite(), it will work, however
(although that defeats the purpose of PHP, i'd like to be able to
randomly access a file :D ).
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=22124&edit=1