www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Needing to match white space during formatted read

reply =?ISO-8859-1?Q?Ali_=C7ehreli?= <acehreli yahoo.com> writes:
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 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
Mar 04 2011
parent =?ISO-8859-1?Q?Ali_=C7ehreli?= <acehreli yahoo.com> writes:
On 03/04/2011 11:21 AM, Ali Çehreli wrote:
 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.
Console interaction is for novices. There are no novices here.
 I would like to know whether the changes that I will be making will be
 correct.
Just make them correctly.
 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")?
That's sounds right.
 2) The equivalent C program does not require a space between format
 specifiers. Is this departure from C intentional?
Forget C. Do D.
 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 std/conv.d(37): std.conv(1161): Can't convert
 value ` 2
 ' of type LockingTextReader to type int
 ----------------
Then just don't pipe.
 That probably needs a bug report but where do you think the difference
 originates from: keyboard, echo, console, D runtime, Phobos, something
 else?
Please file a bug report.
 Thank you,
 Ali
Thank me, Ali
Mar 08 2011