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


Groups > comp.lang.c > #24354

Re: scanf()

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: scanf()
Date 2012-08-07 15:01 -0400
Organization A noiseless patient Spider
Message-ID <502165FF.8040803@verizon.net> (permalink)
References <jvpb53$bo5$1@speranza.aioe.org> <0.8672b74d90d99a7ba880.20120806224900BST.87r4rjzo6r.fsf@bsb.me.uk> <jvrn8u$2df$1@speranza.aioe.org>

Show all headers | View raw


On 08/07/2012 02:37 PM, Edward Rutherford wrote:
> Ben Bacarisse wrote:
> 
>> Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> writes:
>>
>>> If scanf() fails for a matching error, what can we say about the
>>> location of the file pointer for subsequent reads from stdin? Could it
>>> have read on through an indeterminate amount of stdin, or will it
>>> always be positioned immediately after the last successful conversion
>>> from the format string?
>>
>> No, it can read an unlimited number of characters before reporting a
>> matching failure.  There are lots of details, but since you are asking
>> an all-or-nothing question, you probably don't care about them.  A
>> "worst case" example occurs when processing "%d" -- an unlimited number
>> of white-space characters can be read before any matching failure can be
>> reported.  If there is such a failure, the character that causes it
>> matching failure will pushed back onto the stream, but that's all.
>>
>> The scanf functions only ever push back at most one character so you can
>> use that a rule of thumb to imagine what they must consume before a
>> directive can fail.
>>
>> For some input patterns %n is an effective solution, but you don't say
>> what your overall objective is so I can't be sure.
> 
> That's unfortunate, because then there's no way to recover the non-
> matching data, as far as I can see.
> 
> Wouldn't it be better if scanf() buffered non-matching characters, either 
> seemlessly in the background (so that future reads from stdin saw those 
> characters) or by returning a pointer to a buffer containing them?

scanf() was not intended to have that complicated an interface, and it's
far too late to make any changes to its interface now.

If you want to do something like that, the standard library provides you
with the pieces you need to assemble to do it yourself: fgets() from
<stdio.h>  and the strto*() functions from <stdlib.h> are the most
relevant ones.

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


Thread

scanf() Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-06 20:58 +0000
  Re: scanf() Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-06 17:29 -0400
    Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-06 14:40 -0700
  Re: scanf() James Kuyper <jameskuyper@verizon.net> - 2012-08-06 17:39 -0400
  Re: scanf() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-06 22:49 +0100
    Re: scanf() Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-07 18:37 +0000
      Re: scanf() James Kuyper <jameskuyper@verizon.net> - 2012-08-07 15:01 -0400
      Re: scanf() Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-07 15:03 -0400
      Re: scanf() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-07 20:51 +0100
      Re: scanf() John Bode <jfbode1029@gmail.com> - 2012-08-09 14:00 -0700
        Re: scanf() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-11 03:08 -0700
          Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-11 10:40 -0700
            Re: scanf() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-11 13:34 -0700
              Re: scanf() "BartC" <bc@freeuk.com> - 2012-08-11 23:04 +0100
              Re: scanf() James Kuyper <jameskuyper@verizon.net> - 2012-08-11 19:20 -0400
              Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-11 17:30 -0700
                Re: scanf() Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-11 20:41 -0400
                Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-11 18:13 -0700
                Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-11 18:16 -0700
                Re: scanf() Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-11 21:32 -0400
                Re: scanf() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-12 04:07 -0700
                Re: scanf() James Kuyper <jameskuyper@verizon.net> - 2012-08-12 08:03 -0400
                Re: scanf() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-12 06:41 -0700
                Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-12 13:39 -0700
                Re: scanf() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-12 14:20 -0700
                Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-08-12 16:33 -0700
                Re: scanf() "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-13 12:22 +0200
                Re: scanf() Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-09-05 00:13 +0300
                Re: scanf() Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 14:18 -0700
                Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-09-07 15:02 -0700
                Re: scanf() Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 17:25 -0700
                Re: scanf() Keith Thompson <kst-u@mib.org> - 2012-09-07 18:35 -0700
              Re: scanf() Seungbeom Kim <musiphil@bawi.org> - 2012-08-12 21:05 -0700
        Re: scanf() Robert Wessel <robertwessel2@yahoo.com> - 2012-08-11 17:37 -0500

csiph-web