Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: scanf() |
| Date | 2012-08-06 14:40 -0700 |
| Organization | None to speak of |
| Message-ID | <ln1ujjya0w.fsf@nuthaus.mib.org> (permalink) |
| References | <jvpb53$bo5$1@speranza.aioe.org> <jvpd0e$ctd$1@dont-email.me> |
Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> On 8/6/2012 4:58 PM, Edward Rutherford wrote:
>> 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?
>
> The former. For example, consider reading with "%e" and
> encountering the input "123.4567e+---". The first ten characters
> are a valid prefix for a floating-point number, which is then
> spoiled by the eleventh. Yet scanf() can't simply push the
> eleventh character back onto the stream and convert the first
> ten: They are valid as a prefix but not as a complete number.
> scanf() would have to push back three characters ('-', '+', 'e')
> to arrive at something valid, but it has only one character of
> push-back to work with. And, of course, there's simply no way
> it can get all the way back to a position before the '1'.
And that's not the only problem with using scanf to read numeric data.
Consider reading with "%e" and encountering the input "1.0e9999999".
The behavior is undefined -- and since you're reading from stdin,
there's no way (using scanf alone) to avoid that.
(This was something I really hoped C11 would fix, but it didn't.)
The safe way to read floating-point data from stdin is to read lines
using fgets() (or something similar -- obviously not gets()), and then
parse it using strtod().
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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