This works better: import std.stdio; import std.file; void main() { //string s = readText("s.txt"); string s; foreach(line; File("s.txt", "r").byLine()) { s ~= line~"\r\n"; } s = s[0..$-2]; writeln([s]); File("s.txt", "w").write(s); } But one text file program says it has solitary carriage returns in it.