Bug #18006 Updated: more info
| From: | rasmus@php.net | Date: | Thu, 27 Jun 2002 07:19:29 +0000 |
| Subject: | Bug #18006 Updated: more info | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-12211@lists.php.net to get a copy of this message | ||
ID: 18006
Updated by: rasmus@php.net
Reported By: ilia@prohost.org
-Status: Feedback
+Status: Open
Bug Type: Filesystem function related
Operating System: Linux
PHP Version: 4.2.1
New Comment:
No, this is not correct behaviour. Assuming file 'a' does not exist or
is of 0 length when we start, then $s should return 0 on the ftell().
ftruncate() should create a 100 byte file containing nulls *but* by
definition it should not move the file pointer. The file pointer is
still at position 0. So when the fwrite() happens the 100 nulls should
be overwritten by the 100 1's.
Try this test program in C:
#include <stdio.h>
int main() {
FILE *fp;
int s;
fp = fopen("a","a+");
s = ftell(fp);
ftruncate(fp, s+10);
fwrite("1111111111",1,10,fp);
fclose(fp);
}
(didn't feel like typing out 100 1's.)
Looking through Wez's streams code I don't see any obvious bug there.
Previous Comments:
------------------------------------------------------------------------
[2002-06-26 22:50:53] sniper@php.net
What happens here is that you open a file in append mode
and set the filepointer in the end of the file.
Now when you truncate it to 100 bytes, the filepointer
is still in the end. And when you add the 100 bytes, they're
added in the end.
You should open the file in 'w' mode.
AFAIK, this is the correct behaviour.
------------------------------------------------------------------------
[2002-06-26 21:08:26] ilia@prohost.org
Sorry, the 2nd example is wrong, here is the correct one.
<?php
$fp = fopen('a', 'a+');
$s=ftell($fp);
fseek($fp, $s+100, SEEK_SET);
fseek($fp, $s, SEEK_SET);
fwrite($fp, str_repeat('1', 100));
fclose($fp);
?>
------------------------------------------------------------------------
[2002-06-26 21:06:47] ilia@prohost.org
When doing the following in php, the result is a file that is 200
bytes, 100 bytes of NULL and 100 bytes of the actual data.
<?php
$fp = fopen('a', 'a+');
$s=ftell($fp);
ftruncate($fp, $s+100);
fwrite($fp, str_repeat('1', 100));
fclose($fp);
?>
In reality there should be a 100 byte file, full of 1s.
However, this example works
<?php
$fp = fopen('a', 'a+');
$s=ftell($fp);
ftruncate($fp, $s+100, SEEK_SET);
fseek($fp, $s, SEEK_SET);
fwrite($fp, str_repeat('1', 100));
fclose($fp);
?>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=18006&edit=1