digitalmars.D.learn - Reading by character and by line from stdin
- bellinom (9/9) Aug 25 2011 Hello,
- Jonathan M Davis (5/16) Aug 25 2011 I would point out that www.ideone.com is horribly out of date. It uses d...
- bellinom (4/4) Aug 25 2011 Thanks for that, I didn't realize they were that far out of date. I use ...
- Jonathan M Davis (22/25) Aug 25 2011 That's probably not something that has changed, but many bugs have been ...
- Timon Gehr (20/24) Aug 25 2011 current ways
- Timon Gehr (3/28) Aug 25 2011 whoops, this is better:
- Joel Christensen (6/10) Aug 25 2011 Or,
- Timon Gehr (2/14) Aug 25 2011 ... good catch.
- Andrej Mitrovic (6/6) Aug 26 2011 to!(T[]) is awesome.
- Trass3r (13/15) Aug 25 2011 A nifty way of reading by byte is the undocumented (only the writer is
- Trass3r (1/2) Aug 25 2011 Of course I mean by character.
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
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
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
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
On 08/25/2011 11:34 PM, bellinom wrote:Thanks for that, I didn't realize they were that far out of date. Iuse the latestversion of the compiler on my home PC, so I'd like to know the mostcurrent waysof reading from stdin. ThanksCurrently 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
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
On 26-Aug-11 10:20 AM, Timon Gehr wrote:On 08/26/2011 12:19 AM, Timon Gehr wrote: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. - JoelOn 08/25/2011 11:34 PM, bellinom wrote:whoops, this is better: auto arr=to!(int[])(split(strip!(readln())));
Aug 25 2011
On 08/26/2011 06:12 AM, Joel Christensen wrote:On 26-Aug-11 10:20 AM, Timon Gehr wrote:... good catch.On 08/26/2011 12:19 AM, Timon Gehr wrote: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. - JoelOn 08/25/2011 11:34 PM, bellinom wrote:whoops, this is better: auto arr=to!(int[])(split(strip!(readln())));
Aug 25 2011
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
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
A nifty way of reading by byte isOf course I mean by character.
Aug 25 2011