Thread overview | |||||
---|---|---|---|---|---|
|
September 28, 2016 Macintosh text file with invalid line breaks are created | ||||
---|---|---|---|---|
| ||||
Hi, following application creates a text file with strange content: void writeTextFile(string filePath, string text) { import std.stdio: File; auto f = File(filePath, "w"); f.write(text); f.close(); } void main() { import std.ascii: newline; string s = "a=1"~newline~"b=2"; writeTextFile("./test.txt", s); } Notepad++ detects the file as macintosh UTF8 file. The file has following content: a=1CR CRLF b=2 Where the first CR comes from? Is this a bug? dmd v2.071.2 on win10 Kind regards André |
September 28, 2016 Re: Macintosh text file with invalid line breaks are created | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Wednesday, 28 September 2016 at 06:52:51 UTC, Andre Pany wrote:
> Hi,
>
> following application creates a text file with strange content:
>
> void writeTextFile(string filePath, string text)
> {
> import std.stdio: File;
> auto f = File(filePath, "w");
> f.write(text);
> f.close();
> }
>
> void main()
> {
> import std.ascii: newline;
>
> string s = "a=1"~newline~"b=2";
> writeTextFile("./test.txt", s);
> }
>
> Notepad++ detects the file as macintosh UTF8 file.
> The file has following content:
> a=1CR
> CRLF
> b=2
>
> Where the first CR comes from? Is this a bug?
> dmd v2.071.2 on win10
Since the file is opened in text mode (which is the default), the C runtime automatically translates the single \n to a \r\n pair when writing the file. Since std.ascii.newline is defined to be "\r\n" on Windows, it ends up being written as "\r\r\n".
You could either open the file in binary mode (use "wb" as the second argument), or always use "\n" for line separators.
|
September 28, 2016 Re: Macintosh text file with invalid line breaks are created | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | On Wednesday, 28 September 2016 at 16:53:24 UTC, Vladimir Panteleev wrote:
> On Wednesday, 28 September 2016 at 06:52:51 UTC, Andre Pany wrote:
>
> Since the file is opened in text mode (which is the default), the C runtime automatically translates the single \n to a \r\n pair when writing the file. Since std.ascii.newline is defined to be "\r\n" on Windows, it ends up being written as "\r\r\n".
>
> You could either open the file in binary mode (use "wb" as the second argument), or always use "\n" for line separators.
Thank you so much, I was already getting crazy about this. I just checked whether this information is included in library documentation for std.stdio.
If I havent't miss s.th. this isn't mentioned directly but only as a side information in this sentence: Use std.ascii.newline for portability (unless the file was opened in text mode).
I will create a bug report, for users without C knowledge, this is a trap:)
Kind regards
André
|
Copyright © 1999-2021 by the D Language Foundation