|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-01-13 19:34 UTC] wez@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 13:00:01 2025 UTC |
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))))