www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading by character and by line from stdin

reply bellinom <bellinom nsuok.edu> writes:
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
Aug 25 2011
next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Aug 25 2011
parent reply bellinom <bellinom nsuok.edu> writes:
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
Aug 25 2011
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Aug 25 2011
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Aug 25 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Aug 25 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
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
Aug 25 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Aug 25 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
to!(T[]) is awesome.

I've ported this little C thingy:
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);

to this:
to!(float[])(line.split);

Can't get easier than that!
Aug 26 2011
prev sibling parent reply Trass3r <un known.com> writes:
 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.
Aug 25 2011
parent Trass3r <un known.com> writes:
 A nifty way of reading by byte is
Of course I mean by character.
Aug 25 2011