www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading a single whitespace-separated word from stdin

reply "Mark Isaacson" <turck11 hotmail.com> writes:
I'm trying my hand at reading from standard input and having 
little luck. In particular, I would like to be able to do the 
rough equivalent of C++'s:

cin >> myString;

As opposed to reading the whole line.

I attempted to do this with readf:

string result;
readf(" %s ", &result);

However this does not seem to do the trick. If I enter "Hello\n" 
on the terminal, it keeps waiting for input. By contrast, if I 
enter "Hello program", it correctly identifies "Hello" as the 
'result'.

Thus, contrary to what I've been reading, the trailing space 
seems to only account for space characters, not general 
whitespace.

I presume that I'm missing something obvious, any ideas?


Thanks in advance!
May 05 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Mark Isaacson:

 I'm trying my hand at reading from standard input and having 
 little luck. In particular, I would like to be able to do the 
 rough equivalent of C++'s:

 cin >> myString;
There isn't always a 1:1 mapping between C++ and D. In D if you want a single word you usually read the whole line (with a byLine) and then you use split or splitter to take the single words. Something like this (untested): foreach (line; stdin.byLine) { immutable words = line.chomp.idup.split; } Bye, bearophile
May 06 2014
parent reply "Mark Isaacson" <turck11 hotmail.com> writes:
Fair enough. I've done stuff like that in the past. I'm trying to 
implement a university project that was originally designed for 
C++ style I/O... and so where I'd have otherwise jumped at 
something like that from the beginning, my hands are slightly 
tied.

Suppose I'll make due/not fully comply with the spec.
May 06 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Mark Isaacson:

 Fair enough. I've done stuff like that in the past. I'm trying 
 to implement a university project that was originally designed 
 for C++ style I/O... and so where I'd have otherwise jumped at 
 something like that from the beginning, my hands are slightly 
 tied.
If you need/want to use the C++ style, even if it's not fully Phobos-idiomatic, probably you can invent some way to simulate it. Bye, bearophile
May 06 2014
parent reply "Mark Isaacson" <turck11 hotmail.com> writes:
Indeed. However, doing so looks more painful than redefining my 
goals. Upon further examination it seems that I had more 
flexibility than I originally estimated. Besides, the real reason 
I'm implementing this project is just to practice for when I get 
to write production D code in a week anyway; I'd rather learn the 
idioms.
May 06 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Mark Isaacson:

 Indeed. However, doing so looks more painful than redefining my 
 goals. Upon further examination it seems that I had more 
 flexibility than I originally estimated. Besides, the real 
 reason I'm implementing this project is just to practice for 
 when I get to write production D code in a week anyway; I'd 
 rather learn the idioms.
OK. Once you are done with this little project you can post a link here, I can review the code a little if you want. Bye, bearophile
May 06 2014
parent "Mark Isaacson" <turck11 hotmail.com> writes:
An exceptionally generous offer! May take you up on that. Thank 
you :).
May 06 2014