Jump to page: 1 2
Thread overview
Reading by character and by line from stdin
Aug 25, 2011
bellinom
Aug 25, 2011
Jonathan M Davis
Aug 25, 2011
bellinom
Aug 25, 2011
Jonathan M Davis
Aug 25, 2011
Timon Gehr
Aug 25, 2011
Timon Gehr
Aug 26, 2011
Joel Christensen
Aug 26, 2011
Timon Gehr
Aug 26, 2011
Andrej Mitrovic
Aug 25, 2011
Trass3r
Aug 25, 2011
Trass3r
August 25, 2011
Hello,

I'm in the process of learning D, and I have some questions about reading from stdin. I've checked out the documentation but it's really not helping too much. I'd like to know how to read from stdin one character at a time, read with whitespace as a delimiter, and read line by line. Essentially just the equivalents of cin.get(), cin >>, and cin.getline(). At the momoent I'm using the D compiler on www.ideone.com, which I assume uses Phobos.

Thanks in advance for any help.

Malcolm
August 25, 2011
On Thursday, August 25, 2011 10:49 bellinom wrote:
> Hello,
> 
> I'm in the process of learning D, and I have some questions about reading from stdin. I've checked out the documentation but it's really not helping too much. I'd like to know how to read from stdin one character at a time, read with whitespace as a delimiter, and read line by line. Essentially just the equivalents of cin.get(), cin >>, and cin.getline(). At the momoent I'm using the D compiler on www.ideone.com, which I assume uses Phobos.
> 
> Thanks in advance for any help.

I would point out that www.ideone.com is horribly out of date. It uses dmd 2.042, while the latest version is 2.054. So, some stuff will not work there in the same way that they work with the latest release.

- Jonathan M Davis
August 25, 2011
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
August 25, 2011
> I'd like to know how to read from stdin one character at a time, read with whitespace as a delimiter, and read line by line.

A nifty way of reading by byte is the undocumented (only the writer is documented):

import std.stdio;
void main()
{
    foreach(c; LockingTextReader(stdin))
        ....
}

by line is
    foreach(c; stdin.byLine)

and you should also be able to abuse the function to split by whitespace:
http://d-programming-language.org/phobos/std_stdio.html#ByLine
See the terminator parameter.
August 25, 2011
> A nifty way of reading by byte is

Of course I mean by character.
August 25, 2011
On Thursday, August 25, 2011 14:34 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.

That's probably not something that has changed, but many bugs have been fixed, and whole modules have been added or deprecated since 2.042, so it does affect a lot of stuff.

As for exactly how to read from stdin, it's not something that I normally do, so I'm not particularly well-versed in it, but stdin is a std.stdio.File with read-only access, so all of the std.stdio.File operations which read from the file should work. readln reads in a single line, and I believe that there's a version of it in stdio's module scope, so readln by itself should work rather than needing stdin.readln. If you want to read in a particular type, then std.stdio.File.rawRead would be what you would use, I believe, and you can probably use that to read a single character if you want to, but I don't know. There's also readf, which is similar to C's scanf.

All in all, I'd suggest reading the documentation for std.stdio ( http://d- programming-language.org/phobos/std_stdio.html ) and playing around with it a bit, though perhaps someone who is actually familiar with reading from stdin could provide better insight. Personally, the closest that I normally get to operating on stuff from stdin is by operator on program arguments. Most of my programs which operate on I/O, operate on files, and I usually just use std.file.readText for that, since it's nice and simple. But that won't work for stdin.

- Jonathan M Davis
August 25, 2011
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()));

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.


August 25, 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.
>
>

August 26, 2011
On 26-Aug-11 10:20 AM, Timon Gehr wrote:
> On 08/26/2011 12:19 AM, Timon Gehr wrote:
>> On 08/25/2011 11:34 PM, bellinom wrote:
> whoops, this is better:
>
> auto arr=to!(int[])(split(strip!(readln())));
>

Or,
auto arr2=to!(int[])( readln.strip.split );

Got rid of the second ! too (does not work with it). I not sure about having no () for 3 of the functions though.

- Joel
August 26, 2011
On 08/26/2011 06:12 AM, Joel Christensen wrote:
> On 26-Aug-11 10:20 AM, Timon Gehr wrote:
>> On 08/26/2011 12:19 AM, Timon Gehr wrote:
>>> On 08/25/2011 11:34 PM, bellinom wrote:
>> whoops, this is better:
>>
>> auto arr=to!(int[])(split(strip!(readln())));
>>
>
> Or,
> auto arr2=to!(int[])( readln.strip.split );
>
> Got rid of the second ! too (does not work with it). I not sure about
> having no () for 3 of the functions though.
>
> - Joel

... good catch.
« First   ‹ Prev
1 2