digitalmars.D.learn - Reading a single whitespace-separated word from stdin
- Mark Isaacson (17/17) May 05 2014 I'm trying my hand at reading from standard input and having
- bearophile (11/15) May 06 2014 There isn't always a 1:1 mapping between C++ and D. In D if you
- Mark Isaacson (6/6) May 06 2014 Fair enough. I've done stuff like that in the past. I'm trying to
- bearophile (5/10) May 06 2014 If you need/want to use the C++ style, even if it's not fully
- Mark Isaacson (6/6) May 06 2014 Indeed. However, doing so looks more painful than redefining my
- bearophile (5/11) May 06 2014 OK. Once you are done with this little project you can post a
- Mark Isaacson (2/2) May 06 2014 An exceptionally generous offer! May take you up on that. Thank
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
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
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
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
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
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
An exceptionally generous offer! May take you up on that. Thank you :).
May 06 2014