Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is readf not behaving as expected?

Tags:

d

dmd

import std.stdio;

void main(){

  int n;
  while(readf("%d", &n)){
    if(n == 11)
      break;
    writeln(n);
  }
}

The first iteration works, and it prints n, but after that readf() never returns.

The documentation has only a single line explaining readf():

uint readf(A...)(in char[] for­mat, A args);

   For­mat­ted read one line from stdin.

Am I do something wrong? or is there something wrong with readf()? I just need to read numbers from the standard input.

using: DMD 2.054 64-bit

like image 267
Arlen Avatar asked Aug 19 '11 08:08

Arlen


1 Answers

I believe it's because readf handles spaces differently than scanf in C. You need to explicitly read in the spaces, so change readf("%d", &n) to readf("%d ", &n) and it should work (hopefully).

Here's a quote from Andrei, who implemented the function:

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.

http://www.digitalmars.com/d/archives/digitalmars/D/bugs/Issue_4656_New_stdio.readf_does_not_ignore_white_space_24214.html

like image 156
Peter Alexander Avatar answered Sep 19 '22 14:09

Peter Alexander