Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

#32889 [Csd]: ftruncate doesn't move the file pointer
ID:          32889
Updated by:  wez@php.net
Reported By: techtonik@php.net
Status:      Closed
Bug Type:    Documentation problem
PHP Version: Irrelevant
New Comment:

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 sing explicitly if you require it.



Previous Comments:
------------------------------------------------------------------------

[2005-04-29 16:18:32] 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 fs() call is needed."

------------------------------------------------------------------------

[2005-04-29 13:04:11] techtonik@php.net

Something like "Warning: Truncate doesn't change position of the file
pointer. Adjust the pointer with fs 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);
fs($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 12:22:17] tony2001@php.net

"The file pointer is not changed."
(c) man ftruncate

------------------------------------------------------------------------

[2005-04-29 12:12:57] techtonik@php.net

On the second thought it doesn't seem like a feature.

------------------------------------------------------------------------

[2005-04-29 11:36:13] 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");
fs($flog, -2, SEEK_END);
ftruncate($flog, 6);

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

?>

Expected result:
----------------
0000000000: 76 65 65 65 72 79 20 73 │ 68 6F 72 74 73 74 72
veeery shortstr

Actual result:
--------------
0000000000: 76 65 65 65 72 79 20 00 │ 00 00 00 00 00 00 00 00
veeery
0000000010: 00 00 00 00 00 00 00 73 │ 68 6F 72 74 73 74 72
shortstr


------------------------------------------------------------------------


--
Edit this bug report at http://bugs.php.net/?id=32889&edit=1

Report this thread to moderator Post Follow-up to this message
Old Post
wez@php.net
04-29-05 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Documentation archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:09 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.