Remove unwanted ' \r ' from an Array ... by reading a *.txt file.

welkam wwwelkam at gmail.com
Thu Oct 24 16:21:47 UTC 2019


On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote:
> Hi all It's me again ;-)
> (because you're very strong in Dlang, here...)
> In want a result as i write in a comment, with remove unwanted 
> '\r' >>
>
> import std.stdio;
> import std.file;
> import std.conv;
> import std.process : executeShell;
>
> /* Content of 'values.txt' >>
>    abcd
>    1234
>    03b52h
> */
>
> void main() {
> 	auto fichier = File("values.txt", "r+");
> 	string[] buffer;
>   foreach (line ; File("values.txt").byLine)
>     buffer ~= line.to!string;
>
>   writeln(buffer);
>   // Result: ["abcd\r", "1234\r", "03b52h"]
>
>   writeln(buffer[0], " - ", buffer[1], " - ", buffer[2]);
>   // Result:  - 03b52h    :-(
>
>   // Instead, i would :  abcd - 1234 - 03b52h  !!!
>
> executeShell("pause");
> fichier.close();
> }
>
> Thanks a lot, by advance :-)

void main() {
     File("data.txt", "r+")
         .byLineCopy()
         .array()
         .each!writeln;
}

byLineCopy removes the new lines. If in the future you would need 
the new line symbol call byLineCopy(Yes.keepTerminator)


More information about the Digitalmars-d-learn mailing list