Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: scanf used for to get a line |
| Date | Fri, 04 Nov 2016 11:46:21 -0700 |
| Organization | None to speak of |
| Lines | 75 |
| Message-ID | <lnr36rw0aq.fsf@kst-u.example.com> (permalink) |
| References | <15ea8d6d-0173-491c-b6b5-b1c57dd61769@googlegroups.com> <236c25a6-8c18-4082-ab3a-86eb55fb2408@googlegroups.com> <nvfoa6$lsu$1@dont-email.me> <lnfun8zffj.fsf@kst-u.example.com> <5e06af91-eec8-42cd-87c4-99d2b448de84@googlegroups.com> <lnbmxwzab8.fsf@kst-u.example.com> <ba811315-30c5-49f8-b4c6-1a733e7a0ab2@googlegroups.com> <lnpomcxhlt.fsf@kst-u.example.com> <108db18d-12c1-4eaa-afc9-59e7c0ca7798@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="6f57f3023f7434c61f7dba030433622e"; logging-data="7575"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18si+SzG/lp96ydW5bZZ6Bc" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:/1kq2aY/iUZAj0YqMLPI6/Dfcss= sha1:3HNt/d+IO3Q6n/oSq+3/tzK2/qw= |
| Xref | csiph.com comp.lang.c:95785 |
Show key headers only | View raw
supercat@casperkitty.com writes:
> On Thursday, November 3, 2016 at 6:35:00 PM UTC-5, Keith Thompson wrote:
>> supercat writes:
>> > Before using strtoll, one would need to get the input into a string and
>> > then find where each part starts.
>>
>> No, you just need to get the input into a string (fgets() will do that,
>> or something more sophisticated if you can't place an upper bound on the
>> input line length). strtoll will tell you where in the string the
>> number ends if you ask it to.
>
> The Standard the scenarios in which a newline is encountered, no newline
> is encountered before enough characters are read, the end of the file is
> encountered *and no characters have been read into the array*, or a
> read-error occurs. I see nothing that would characterize EOF as a read
> error, nor do I see anything that would describe the behavior in case
> an EOF is encountered after characters have been read. Those factors put
> together would seem to suggest that nasal demons in such a scenario would
> not make an implementation non-conforming.
Can you be more specific about the scenario you're talking about?
What I'm suggesting is:
1. Read a line into a string using fgets(), or something more
sophisticated if you want to handle arbitrarily long lines. The
resulting string may or may not have a trailing '\n', and it may or may
not be empty.
2. Use strtoll() to scan long long values from the string, storing them
in objects of type long long.
I see no possibility for nasal demons if the above is done correctly.
[...]
>> > It's also not hard to imagine a version of "scanf" which, if input was
>> > coming from a console, might inform the user if an attempt was made to
>> > enter invalid data. Such notification might be done in ways outside the
>> > jurisdiction of the C Standard (triggering a buzzer, for example).
>>
>> That doesn't seem plausible. The idea of treating standard input
>> consistently regardless of where it's coming from is quite old. I don't
>> think even a very old implementation of scanf would have triggered a
>> buzzer on an error.
>
> It would be implausible that a Unix implementation would do such a thing.
> Not all systems support console redirection, however. On many systems
> which don't support console redirection, console input *must* be treated
> differently from file input, at least at the OS level.
The early history of scanf, like that of C, is largely Unix-specific.
It was originally defined and implemented on Unix. I presume that
its behavior on other systems, for early ports, would have been
designed to emulate the Unix behavior. I'd be surprised if scanf()
were ever designed to trigger a buzzer on any system to which it
was ported. (Feel free to present a real-world counterexample.)
It already has error-reporting facilities, and any interactive
response to an error should be up to the program, not to the
library routine.
Not that any of this really matters. Regardless of the history,
the fact that the *scanf() functions have undefined behavior on
numeric overflow is, in my opinion and I think in yours, a flaw
in the standard that should be corrected. Treating overflow as a
matching failure is probably the simples approach. The fact that
the strto*() functions (which I think are newer than *scanf()) are
reqiured to handle numeric overflow gracefully strongly suggests
that such a change would not be a burden on implementers.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"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 used for to get a line asetofsymbols@gmail.com - 2016-11-01 01:50 -0700
scanf used for to get a line asetofsymbols@gmail.com - 2016-11-01 01:55 -0700
Re: scanf used for to get a line mark.bluemel@gmail.com - 2016-11-01 03:55 -0700
Re: scanf used for to get a line "Charles Richmond" <numerist@aquaporin4.com> - 2016-11-03 11:23 -0500
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 09:38 -0700
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-03 10:08 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 11:29 -0700
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-03 14:37 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 16:34 -0700
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-04 10:21 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-04 11:46 -0700
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-04 15:04 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-04 16:44 -0700
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-04 17:13 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-04 19:12 -0700
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-05 14:10 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-05 14:22 -0700
Re: scanf used for to get a line Tim Rentsch <txr@alumni.caltech.edu> - 2016-11-15 10:57 -0800
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-15 12:05 -0800
Re: scanf used for to get a line jameskuyper@verizon.net - 2016-11-15 14:01 -0800
Re: scanf used for to get a line Tim Rentsch <txr@alumni.caltech.edu> - 2016-11-16 10:54 -0800
Re: scanf used for to get a line Richard Damon <Richard@Damon-Family.org> - 2016-11-21 18:01 -0500
Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-21 15:44 -0800
Re: scanf used for to get a line asetofsymbols@gmail.com - 2016-11-03 13:35 -0700
Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 13:50 -0700
Re: scanf used for to get a line ImperiusDamian <damianblack.wolf@gmail.com> - 2016-11-05 16:10 -0400
Re: scanf used for to get a line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-01 11:39 +0000
Re: scanf used for to get a line asetofsymbols@gmail.com - 2016-11-01 05:47 -0700
Re: scanf used for to get a line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-01 15:05 +0000
Re: scanf used for to get a line asetofsymbols@gmail.com - 2016-11-02 01:42 -0700
csiph-web