Needing to match white space during formatted read

Ali Çehreli acehreli at yahoo.com
Fri Mar 4 11:21:19 PST 2011


I am asking this question from the point of view of someone who is in 
the process of replacing std.cstream with std.stdio in his book that 
targets novices. There are many sample programs in the book where the 
user interacts with the program through the console.

I would like to know whether the changes that I will be making will be 
correct.

import std.stdio;

void main()
{
     int i;
     int j;

     readf("%d%d", &i, &j);
}

1) The code above does not terminate when I interact with the program at 
the console and enter "1 2" from the keyboard (without the double quotes)

I understand that the reason is the space character that I had to type 
between the two values. The solution is to use a format string that must 
take account for that white space.

Does that mean that we must always use spaces before format specifiers 
as in " %d %d" (and even better: " %s %s")?

2) The equivalent C program does not require a space between format 
specifiers. Is this departure from C intentional?

#include <stdio.h>

int main()
{
     int i = 0;
     int j = 0;

     scanf("%d%d", &i, &j);
}

I think D is more consistent here, but the difference in behavior is 
non-trivial.

3) The program above behaves differently when the input is piped from 
the output of another program:

$ echo '1 2' | ~/deneme/d/deneme
std.conv.ConvException at std/conv.d(37): std.conv(1161): Can't convert 
value ` 2
' of type LockingTextReader to type int
----------------

That probably needs a bug report but where do you think the difference 
originates from: keyboard, echo, console, D runtime, Phobos, something else?

Thank you,
Ali


More information about the Digitalmars-d mailing list