php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32090 fopen(..., 'wt') - fwrite($fp, ...) translates \n to \r
Submitted: 2005-02-24 00:02 UTC Modified: 2005-03-05 01:00 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: ceefour at gauldong dot net Assigned:
Status: No Feedback Package: Filesystem function related
PHP Version: 5.0.3 OS: Windows XP SP1
Private report: No CVE-ID: None
 [2005-02-24 00:02 UTC] ceefour at gauldong dot net
Description:
------------
Using 'wt' (write-text), any \n should be translated to \r\n (0D 0A).

However in my system, PHP 5.0.3 in Windows XP SP1, this mode translates \n to \r only (0D), which results in MAC-style line endings.

I had to use 'wb' (write-binary) and put \r\n manually to get desired results.

Reproduce code:
---------------
$fp = fopen('anyfile.txt', 'wt');
fwrite($fp, "Hello\n");
fclose($fp);

Expected result:
----------------
anyfile.txt contains "Hello" + 0D 0A (\r \n)

Actual result:
--------------
anyfile.txt contains "Hello" + 0D (\r only)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-24 00:10 UTC] ceefour at gauldong dot net
BTW, for *PORTABILITY* I choose 't' (text mode), so it works both for UNIXes and Windows, even MACs.

I don't want to do this:
fopen(...., 'wb')
if (strpos('win', strtolower(PHP_OS)) !== false) {
  $line_ending = "\r\n";
} else {
  $line_ending = "\n";
}

which is NOT PORTABLE AT ALL. Why do you suggest 'b' mode will be more portable? The documentation is weird, IMHO.
 [2005-02-24 01:52 UTC] wez@php.net
Please provide a complete test case; most of these so called bug reports are due to the person viewing the data using the wrong tools.

FYI: "text mode" is an evil invention that usually leads to trouble.  It is generally better for the programmer to be explicit with their line endings, as it leads to a lower WTF? factor.
 [2005-02-24 03:23 UTC] ceefour at gauldong dot net
Hi Mr. Furlong! Thanks for the feedback.

The test case is complete. And I'm viewing it using a hex editor (doesn't matter which). The "Hello\n" will output exactly 6 bytes, the Hello and the 0D (\r). It's a MAC document, as detected by a good text editor. If you try to output more lines, i.e., "Hello\nWorld", the text will get concatenated if you try to use Notepad. Viewing it in WordPad or any other good text editor works fine.

>FYI: "text mode" is an evil invention that usually leads to trouble.

I agree with you. But it doesn't solve the problem. As stated in my previous problem, I can write code which use explicit line endings, but that will require me to write "if" statements for who-knows-how-many platforms I will need to support. It's easier that way.

Do you know setlocale() ? Yeah, try EXPLICITLY specifying your locale-specific formats without using strftime()-s formats and/or setlocale(). Things like this IMHO should be handled at lower levels of abstraction. And even if not, the lower level library should give the programmer a choice, i.e.: "we recommend you NOT to use it but if you use it we guarantee it will work correctly". In this case 'wt' doesn't work correctly.

BTW I can even e-mail you the exact script, the exact generated file, and even my entire PHP installation if you want to. ;-)

BTW in case you're wondering I use the mbstring extension with UTF-8 internal encoding specified in php.ini... but it shouldn't matter, should it?
 [2005-02-25 14:28 UTC] sniper@php.net
Please try using this CVS snapshot:

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

And yes, mbstring might be the one causing trouble. Always try disabling everything possible.

 [2005-03-05 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 04 20:01:28 2024 UTC