Reading by character and by line from stdin

Jonathan M Davis jmdavisProg at gmx.com
Thu Aug 25 15:16:08 PDT 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


More information about the Digitalmars-d-learn mailing list