Reading whitespace separated strings from stdin?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 20 19:42:02 PDT 2015


On Tuesday, 21 April 2015 at 02:04:24 UTC, TheGag96 wrote:
> It'll just leave some trailing whitespace, which I don't want.

oh it also keeps the newlines attached. Blargh.

Well, forget the D functions, just use the C functions:

import core.stdc.stdio;

void main() {
         char[16] token;
         while(scanf("%15s", &token) != EOF)
                 printf("**%s**\n", token.ptr);
}



You could convert to a string if needed with import std.conv; 
to!string(token.ptr), but if you can avoid that, you should, this 
loop has no allocations which is a nice thing.


More information about the Digitalmars-d-learn mailing list