Reading by character and by line from stdin
Timon Gehr
timon.gehr at gmx.ch
Thu Aug 25 15:20:22 PDT 2011
On 08/26/2011 12:19 AM, Timon Gehr wrote:
> On 08/25/2011 11:34 PM, bellinom wrote:
> > Thanks for that, I didn't realize they were that far out of date. I
> use the latest
> > version of the compiler on my home PC, so I'd like to know the most
> current ways
> > of reading from stdin.
> >
> > Thanks
>
> Currently what you get is readf and readln with std.conv.to, and if you
> need speed for formatted reads, use the C function scanf.
>
>
> Some examples:
>
> import std.stdio;
>
> read a single line:
>
> string r=readln();
>
> read array of whitespace-delimited integers on a single line:
>
> auto arr=to!(int[])(strip!(readln()));
whoops, this is better:
auto arr=to!(int[])(split(strip!(readln())));
>
> read all of stdin by line:
>
> foreach(s; stdin.byLine){
> // s is the current line
> }
>
> int i;
> readf(" %s",&i); // read i, skipping leading whitespace
>
> with readf, you can always use %s in your format strings.
>
> Don't use readf if you care for performance, because the current
> implementation is very slow.
>
>
More information about the Digitalmars-d-learn
mailing list