Bug 4656 – stdio.readf does not ignore white space
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-08-16T14:11:00Z
Last change time
2015-11-07T01:29:35Z
Assigned to
andrei
Creator
acehreli
Comments
Comment #0 by acehreli — 2010-08-16T14:11:35Z
This makes it very difficult to read values. We can't retire din.readf before fixing this.
import std.stdio;
void main()
{
int i, j;
readf("%s", &i);
readf("%s", &j);
}
Run the program and provide the following as input:
42 43
The program terminates with
std.conv.ConvError: std.conv(1070): Can't convert value `LockingTextReader(File(807637C), )' of type LockingTextReader to type int
The reason is the white space.
Comment #1 by andrei — 2011-05-09T09:18:02Z
This is by design. The example works when modified as follows:
import std.stdio;
void main()
{
int i, j;
readf("%s", &i);
readf(" %s", &j);
}
The space before the second parameter tells readf to read and skip all whitespace before attempting conversion.
I've implemented readf to be a fair amount more Nazi about whitespace than scanf in an attempt to improve its precision. Scanf has been famously difficult to use for complex input parsing and validation, and I attribute some of that to its laissez-faire attitude toward whitespace. I'd be glad to relax some of readf's insistence on precise whitespace handling if there's enough evidence that that serves most of our users. I personally believe that the current behavior (strict by default, easy to relax) is best.
Comment #2 by acehreli — 2011-05-09T10:42:43Z
My apologies for forgetting about this bug report. I have since then replaced din and dout with stdin and stdout in D.ershane. The nazi whitespace behaviour worked correctly and consistently in all of the examples.
Thank you,
Ali
Comment #3 by andrei — 2011-05-09T11:27:57Z
Let's keep it opened until the error message is fixed.
Comment #4 by lovelydear — 2012-04-21T15:22:11Z
The original test case now produces this message:
PS E:\DigitalMars\dmd2\samples> rdmd bug.d
456 45646
std.conv.ConvException@E:\DigitalMars\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1779): Unexpected ' ' when converting from type LockingTextReader to type int
Comment #5 by dlang-bugzilla — 2015-11-07T00:54:06Z
(In reply to Andrei Alexandrescu from comment #3)
> Let's keep it opened until the error message is fixed.
What is there to be fixed?(In reply to SomeDude from comment #4)
> The original test case now produces this message:
"Unexpected ' '" seems like an improvement, so I think this can now be closed.