Bug 9750 – byLine(KeepTerminator.no) problem with Windows newlines in binary mode files

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-03-18T11:20:00Z
Last change time
2013-03-18T12:11:08Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-03-18T11:20:32Z
I have a text file "words.txt" with lines separated by Windows newlines: word1 word2 word3 This code: import std.stdio; void main() { File("words.txt") .byLine(KeepTerminator.no) .writeln; } Prints: ["word1\r", "word2\r", "word3"] I think the problem comes from File() opening on default in binary mode, so each line (but the last one) ends with "\r\n", and byLine(KeepTerminator.no) is stripping away only '\n' leaving the '\r'. I think a D user assumes KeepTerminator.no should remove both if present. std.string.chomp() removes both if they are present.
Comment #1 by andrej.mitrovich — 2013-03-18T11:57:20Z
(In reply to comment #0) > import std.stdio; > void main() { > File("words.txt") > .byLine(KeepTerminator.no) > .writeln; > } I think std.stdio is badly designed w.r.t. newlines. A terminator can be set (in the call to byLine), however it can only be a character instead of a string. So '\n' or '\r' is fine, but you can't set "\r\n".
Comment #2 by dlang-bugzilla — 2013-03-18T12:01:02Z
Dupe of issue 5378?
Comment #3 by andrej.mitrovich — 2013-03-18T12:11:08Z
Yeah I think it's a dupe. *** This issue has been marked as a duplicate of issue 5378 ***