php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #32889 ftruncate doesn't move the file pointer
Submitted: 2005-04-29 11:36 UTC Modified: 2007-09-02 21:43 UTC
From: techtonik@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: techtonik@php.net
New email:
PHP Version: OS:

 

 [2005-04-29 11:36 UTC] techtonik@php.net
Description:
------------
Note or warning here?

http://www.php.net/manual/en/function.ftruncate.php

Reproduce code:
---------------
<?php

// truncate doesn't move the file pointer to the end of the file

$flog = fopen("text.txt", "w+");
fwrite($flog, "veeery veeery long string");
fseek($flog, -2, SEEK_END);
ftruncate($flog, 6);

fwrite($flog, "shortstr");
fclose($flog);

?>

Expected result:
----------------
0000000000: 76 65 65 65 72 79 20 73 &#9474; 68 6F 72 74 73 74 72     veeery shortstr

Actual result:
--------------
0000000000: 76 65 65 65 72 79 20 00 &#9474; 00 00 00 00 00 00 00 00  veeery
0000000010: 00 00 00 00 00 00 00 73 &#9474; 68 6F 72 74 73 74 72            shortstr

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-29 12:12 UTC] techtonik@php.net
On the second thought it doesn't seem like a feature.
 [2005-04-29 12:22 UTC] tony2001@php.net
"The file pointer is not changed."
(c) man ftruncate
 [2005-04-29 13:04 UTC] techtonik@php.net
Something like "Warning: Truncate doesn't change position of the file pointer. Adjust the pointer with fseek to write to the end of the file after truncate operation in modes other than "a" and "a+"." ?

Examples:
<?php

$fh = fopen("file.txt","w+");
fwrite($fh, "newfile");  // file contains string 'newfile'
ftruncate($fh, 3);
fseek($fh, 0, SEEK_END);
fwrite($fh, "contents"); // file contains 'newcontents'
fclose($fh);            

?>

<?php

$fh = fopen("file.txt","w+");
fwrite($fh, "newfile");  // file contains string 'newfile'
ftruncate($fh, 3);

fwrite($fh, "contents"); // file contains 'new    contents'
fclose($fh);            

?>
 [2005-04-29 16:18 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"The file pointer is changed only in append mode. In write mode, additional fseek() call is needed."
 [2005-04-29 18:31 UTC] wez@php.net
The file pointer is not changed at all by PHP when you truncate.
This is consistent with ANSI ftruncate (man ftruncate).

You are responsible for seeking explicitly if you require it.

 [2005-04-30 10:51 UTC] techtonik@php.net
Tnx. Now everything is clear.
 [2007-09-02 21:43 UTC] bjori@php.net
The previous fix was wrong.
The docs have been updated to "The file pointer is <emphasis>not</emphasis> changed."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 20:01:45 2024 UTC