Bug 18962 – readln() removes all '\r' characters from stdin

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2018-06-09T18:35:38Z
Last change time
2018-06-09T20:25:36Z
Assigned to
No Owner
Creator
Laurent Tréguier

Comments

Comment #0 by laurent.treguier.sink — 2018-06-09T18:35:38Z
import std.conv; import std.stdio; void main() { auto line = readln(); while (!stdin.eof) { auto res = stdin.rawRead(new char[1]); if (res.length) { stdout.writeln(res[0].to!int); } } } ------------------------ This code will remove any '\r' character coming from stdin. Using any file with CRLF line endings as input, the LF part will be outputed (as its ascii code 10), but not the CR part. Commenting the `auto line = readln();` line makes `stdin.rawRead()` read '\r' characters properly; the CR part will be outputed (as its ascii code 13). This doesn't seem to occur on Linux. DMD and Phobos version : 2.080.1
Comment #1 by schveiguy — 2018-06-09T20:25:36Z
This is due to libc on windows attempting to fix the line endings for you. There's not much D can do about it, since it relies on FILE * to do its i/o. Note that the reason the single readln affects things is likely because line-endings are altered when the buffer is filled (which is done with one large read). All future reads can't recover the removed CRs. If you want the given behavior, set the mode to binary before doing anything: _setmode(stdin.fileno, _O_BINARY); The reason it doesn't occur on Linux is because Linux doesn't do anything in binary mode.