formattedRead whitespace quirks (compared to scanf)

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Dec 25 16:22:16 PST 2013


On 12/25/13 1:06 PM, Ali Çehreli wrote:
> On 12/25/2013 12:43 PM, Gordon wrote:
>
>  > -- in C --
>  > char a[100] = {0};
>  > chat *input = "hello world 42";
>  > sscanf(input, "%s", &a);
>  > -- in D --
>  > string a;
>  > string input = "hello world 42";
>  > formattedRead(input,"%s", &a);
>  > -----------
>  >
>  > In "C", the variable "a" would contain only "hello";
>  > In "D", the variable "a" would contain "hello world 42";
>  >
>  > BUT,
>  > If the format string would be "%s %s %d" (and we had three variables),
>  > then "formattedRead()" would behave exactly like "sscanf()".
>
> That is by design. Since a string can contain space characters, the
> normal behavior is to read everything as a part of the the string. scanf
> is defined differently.

Yah, that's intentional. scanf has its usefulness slashed to a fraction 
because of the way it handles strings. People added %[...] to compensate 
for that; I chose to just fix it.

Andrei




More information about the Digitalmars-d mailing list