php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28363 fwrite fails when fopen ASCII file under Windows with 'wt' flags
Submitted: 2004-05-12 02:55 UTC Modified: 2004-05-13 04:17 UTC
From: zoltan at frombach dot com Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 4.3.6 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: zoltan at frombach dot com
New email:
PHP Version: OS:

 

 [2004-05-12 02:55 UTC] zoltan at frombach dot com
Description:
------------
In ASCII mode, fwrite under Windows (when the file is opened with the 'wt' flags) will fail. This is a reproducable bug. See my demonstration program.

When "\n" (new line) characters written out to the file, those characters are automatically replaced with "\r\n" which is the expected behaviour in ASCII mode on Windows (when the file is opened with the 't' flag). However, the next fwrite will overwrite the last few characters that were previously written to the file!! Exactly that many characters get lost as many "\r" characters were inserted to the file on the previous fwrite. Seems to me that after the "\n" to "\r\n" translation, the current file position pointer does not get updated, so the next fwrite will overwrite the last few characters previously written to the file.

If there is only one fwrite (if you comment out the second fwrite from my code) then no characters get lost!! Because if there is no further writing to the file, no character overwriting occures. That's why I guess it is a file pointer problem. In other words, it's not the first fwrite that forgets to write out those few last characters, IMHO it just forgets to properly update the actual file pointer and that's why the next fwrite overwrites those last few characters in the file.

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

$fp=fopen('testfile.txt', 'wt');
fwrite($fp, "This\ntest\nwill\nfail\n");
fwrite($fp, "Under\nWindows\nmost\ndefinitely\n");
fclose($fp);

?>


Expected result:
----------------
This
test
will
fail
Under
Windows
most
definitely


Actual result:
--------------
This
test
will
faUnder
Windows
most
definitely


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-12 12:48 UTC] wez@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-STABLE-latest.zip

Please test the next snapshot (dated after this report).
I've committed something that should solve this problem.
 [2004-05-13 00:29 UTC] zoltan at frombach dot com
You have fixed the problem. It works with the current ( http://snaps.php.net/win32/php4-win32-STABLE-200405122030.zip ) CVS snapshot. Thanks!!
 [2004-05-13 02:25 UTC] wez@php.net
Be warned that seeking on a text mode stream will lead to similar results to those you reported; try to avoid it (and text mode streams in general!).
 [2004-05-13 04:17 UTC] zoltan at frombach dot com
I *never* seek in text files. I'm just generating a new text file in one single pass. I fopen the text file for writing only with the 'wt' flags and not the 'w+t' flags.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 17:01:30 2024 UTC