ID: 31540
Updated by: [EMAIL PROTECTED]
Reported By: mike at cocoweb dot co dot uk
-Status: Open
+Status: Bogus
Bug Type: Strings related
Operating System: Windows XP
PHP Version: 5.0.2
New Comment:
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.
"
Previous Comments:
------------------------------------------------------------------------
[2005-01-13 16:43:42] 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))))
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=31540&edit=1