|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-06-26 21:06 UTC] ilia at prohost dot 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);
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Jan 08 12:00:01 2026 UTC |
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); ?>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.