read single characters from stdin

Ali Çehreli acehreli at yahoo.com
Wed Sep 26 11:51:34 PDT 2012


On 09/26/2012 10:51 AM, Thomas Koch wrote:
 > Hi,
 >
 > to learn D, I'd like to write a simple type trainer. It should write 
a line
 > to stdout and then read single characters from stdin and only accept the
 > correct characters
 >
 > How can I read single characters?
 >
 > A similar question has been asked before without a working answer:
 > http://forum.dlang.org/thread/jl79f7$2083$1@digitalmars.com

Considering that stdin is a char stream and that there is no concept of 
a keyboard or a monitor in D (nor in C and nor in C++), I stand by my 
solution from that thread: :)

import std.stdio;

void main()
{
     while (!stdin.eof) {
         char code;
         readf("%s", &code);
         writefln("%u", code);
     }
}

Just remember that char is a UTF-8 code unit.

 > But I can't beliebe that there isn't a simple solution in D for this
 > problem...

This thread may have some answers:

   http://forum.dlang.org/thread/im8cnf$ioe$1@digitalmars.com?page=1

Ali



More information about the Digitalmars-d-learn mailing list