Re^2: scanf(..)

Niels J|rgen Kruse njk at freja.diku.dk
Tue Jun 13 05:33:11 AEST 1989


scs at adam.pika.mit.edu (Steve Summit) writes:

>Scanf's most miserable problem is that it doesn't discard
>unrecognized input.  (This is a documented feature, so don't try
>getting it changed.  It is only a miserable problem when scanf
>is being used for interactive user input, which is what everybody
>uses it for.)  The 'a' sits there in the input buffer, causing
>each scanf to fail, and the correct digits you probably typed
>after the 'a' are never seen.

But if you want scanf to skip bad input, you can just tell it to do
so. In this case you could write
...
  scanf ("%*[^0123456789] %d",&d)
...
which will cause scanf to discard leading junk and then read
a number.
A skip-leading-junk conversion modifier would be nice though.
Perhaps a '!' could be used for that : scanf ("%!d",&d) .
-- 
         Niels J|rgen Kruse
Email    njk at diku.dk
Mail     Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark



More information about the Comp.lang.c mailing list