php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31540 fwrite baboonacy
Submitted: 2005-01-13 16:43 UTC Modified: 2005-01-13 19:34 UTC
From: mike at cocoweb dot co dot uk Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.0.2 OS: Windows XP
Private report: No CVE-ID: None
 [2005-01-13 16:43 UTC] mike at cocoweb dot co dot uk
Description:
------------
fwrite() seems to behave incorrectly when using fopen with the "wt" flag on windows...

If you open a file using fopen and "w" the supplied code works as expected, by placing \0x0A characters for \n.  Which is correct.  However the fopen documentation clearly says to use "wt" on windows for text files...

If you do, then fwrite() attempts to write all the characters, but because it counts the '\n' characters once, it gets to the end of the string early (zero terminator)... i.e. before it has finished writing all the bytes that it should have.


Reproduce code:
---------------
$fp = fopen("bob.txt", "wt")

// This is the line I'd expect to work for files
// opened in "wt" mode:
fwrite($fp, "hello\n\n\n", 11)

(11 is intentional, as in "wt" mode, the \n's are two bytes,
and hence the fwrite() should be outputting two bytes for each
'\n'), which it does.  It just counts the string incorrectly in "wt" mode.


Expected result:
----------------
Binary output:

'h' 'e' 'l' 'l' 'o' \0x0d \0x0a \0x0d \0x0a \0x0d \0x0a


Actual result:
--------------
Binary output:

'h' 'e' 'l' 'l' 'o' \0x0d \0x0a \0x0d

The Dialogue:

Mr. fwrite: "I've finished cos I found a \0x00 on the input string..."

Developer: "err... no you haven't finished, wasn't your file
opened in 'wt' mode, and therefore you need to count \n as two bytes?"

Mr. fwrite: "Oh I forgot about that".

Developer: "Doh!".

I am expecting a reply such as:
Mr. PHP.net: "You have forgotten something obvious Mr. Developer" ;o))))


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-13 19:34 UTC] wez@php.net
I can't reproduce the effects you described, and you're using fwrite() incorrectly; there are not 11 characters in the string you pass to it, only 8.

The fopen() docs clearly tell you to avoid text-mode:
"
Note:  Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the 't'  mode so that it uses the correct line endings and 'b' mode instead.
"

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 04:01:28 2024 UTC