Bug 12493 – std.file.readText doesn't convert Windows newlines correctly
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2014-03-30T07:16:00Z
Last change time
2014-04-04T10:34:08Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-03-30T07:16:32Z
If I create a text file named "text.txt" like this, that contains four lines, and I save it with Windows newlines:
this
is
a
test
Then if I read and print the whole file like this:
void main() {
import std.stdio: writeln;
import std.file: readText;
auto t = readText("text.txt");
writeln(t);
}
Output:
this
is
a
test
Expected output:
this
is
a
test
Comment #1 by yebblies — 2014-04-04T07:28:10Z
stdout is opened in text mode, so it converts all \n to \r\n giving \r\n\n
Everything is working as designed, as terrible as that design might be.
Comment #2 by dlang-bugzilla — 2014-04-04T08:40:16Z
I can't reproduce this issue on my machine.
> stdout is opened in text mode, so it converts all \n to \r\n giving \r\n\n
I thought text mode converted both DOS and UNIX newlines to OS newlines, so it shouldn't double-convert?
Comment #3 by dlang-bugzilla — 2014-04-04T08:42:03Z
If I redirect the output to a file, I get \r\r\n newlines, not \r\n\n. That makes sense, I don't know where \r\n\n come from.
Consecutive \r symbols are a no-op, so I don't see why it would print empty lines between text lines.
Comment #4 by yebblies — 2014-04-04T08:54:29Z
(In reply to comment #3)
> If I redirect the output to a file, I get \r\r\n newlines, not \r\n\n. That
> makes sense, I don't know where \r\n\n come from.
>
Oops, that's what I meant. It converts \n to \r\n, so the original \r\n becomes \r\r\n
> Consecutive \r symbols are a no-op, so I don't see why it would print empty
> lines between text lines.
What \r does depends on your terminal.
Comment #5 by dlang-bugzilla — 2014-04-04T08:56:55Z
(In reply to comment #4)
> What \r does depends on your terminal.
What terminal prints a newline when it sees \r? A 90's Macintosh?
Comment #6 by yebblies — 2014-04-04T08:59:26Z
(In reply to comment #5)
> (In reply to comment #4)
> > What \r does depends on your terminal.
>
> What terminal prints a newline when it sees \r? A 90's Macintosh?
I'd be very impressed if that's what bearophile is running windows on.
Comment #7 by dlang-bugzilla — 2014-04-04T09:01:34Z
Windows has a very defined meaning of what \r means. It means, "move the caret/cursor to the beginning of the line" (\r is CR for Carriage Return, \n is LF for Line Feed). I recall that my matrix printer interpreted these control characters in the same way.
Which is why I'm curious how bearophile got that program to print interleaving lines in the first place.
Comment #8 by bearophile_hugs — 2014-04-04T10:20:54Z
(In reply to comment #7)
> Which is why I'm curious how bearophile got that program to print interleaving
> lines in the first place.
Let's see. If I add a print of the bytes, like this:
void main() {
import std.stdio;
import std.file: readText;
auto t = readText("text.txt");
writef("%(%d %)", t);
}
The output is:
116 104 105 115 13 10 105 115 13 10 97 13 10 116 101 115 116
Is your output the same?
Comment #9 by dlang-bugzilla — 2014-04-04T10:22:40Z
Yes.
Comment #10 by bearophile_hugs — 2014-04-04T10:34:08Z
(In reply to comment #9)
> Yes.
OK. So I presume it's my terminal that acts a little strangely. Thank you all for the answers, and sorry for the invalid report.