Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99352

Re: sscanf error checking

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: sscanf error checking
Date 2016-12-14 16:40 +0000
Organization A noiseless patient Spider
Message-ID <87inqm79ba.fsf@bsb.me.uk> (permalink)
References <o2rp71$3e7$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> writes:

> I got the impression, according to the 'while-loop reading from file'
> thread (where I was strongly chastised for being lax with
> error-handling), that 'sscanf' was /the/ way to detect errors in
> numerical input.
>
> But then I come across this:
>
>   double x;
>   int status;
>   char* str = "123..456";
>
>   status = sscanf(str,"%lf",&x);
>
>   printf("Status = %d\n",status);
>   printf("X      = %f\n",x);
>
> Output:
>
>   Status  = 1
>   X       = 123.000000
>
> The same when scanning "123GARBAGE". It's just stopping when it sees
> something that can't be part of the number without reporting anything
> amiss. Which is not far off why I do.

Yes, it's similar to what you did in you recent example.  The main
difference is the sscanf reports success or failure and you discard that
information.

> Am I doing something wrong or is sscanf not as great at verifying
> input as is made out?

You need to check the input.  sscanf just tells you it succeeded in
finding a number.  It can't report a failure here because it does not
know what you want.  You might want to pick the number off the front of
some alphanumeric code, or you may want treat anything following the
number as a sign of failure.  If you want the latter semantics, you can
use %n:

  sscanf(str, "%lf%n", &result, &len) == 1 && str[len] == 0

(Another strategy is to read a character and examine it, but that's more
fussy to write.)

But the strtoX functions are better for that this simple (much better in
that they will detect overflow).

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sscanf error checking BartC <bc@freeuk.com> - 2016-12-14 15:42 +0000
  Re: sscanf error checking John Gordon <gordon@panix.com> - 2016-12-14 15:58 +0000
    Re: sscanf error checking Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-14 11:14 -0500
  Re: sscanf error checking gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-14 16:11 +0000
    Re: sscanf error checking BartC <bc@freeuk.com> - 2016-12-14 17:51 +0000
      Re: sscanf error checking gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-14 18:09 +0000
      Re: sscanf error checking Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 19:49 +0000
        Re: sscanf error checking BartC <bc@freeuk.com> - 2016-12-14 20:43 +0000
  Re: sscanf error checking Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 16:40 +0000
  Re: sscanf error checking Keith Thompson <kst-u@mib.org> - 2016-12-14 09:34 -0800
  Re: sscanf error checking Robert Wessel <robertwessel2@yahoo.com> - 2016-12-14 14:52 -0600
    Re: sscanf error checking supercat@casperkitty.com - 2016-12-15 07:24 -0800
      Re: sscanf error checking Keith Thompson <kst-u@mib.org> - 2016-12-15 11:26 -0800
        Re: sscanf error checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-15 15:35 -0800
          Re: sscanf error checking Keith Thompson <kst-u@mib.org> - 2016-12-15 16:28 -0800
            Re: sscanf error checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-15 16:37 -0800
        Re: sscanf error checking supercat@casperkitty.com - 2016-12-15 16:38 -0800
      Re: sscanf error checking raltbos@xs4all.nl (Richard Bos) - 2016-12-15 20:20 +0000
        Re: sscanf error checking Keith Thompson <kst-u@mib.org> - 2016-12-15 12:33 -0800
  sscanf error checking asetofsymbols@gmail.com - 2016-12-16 01:49 -0800
    sscanf error checking asetofsymbols@gmail.com - 2016-12-16 01:53 -0800

csiph-web