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


Groups > comp.lang.c > #42382

Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.c
Subject Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead?
Date 2014-03-30 22:49 -0400
Organization A noiseless patient Spider
Message-ID <lhal43$n7r$1@dont-email.me> (permalink)
References <c6584a57-8f78-4974-bd65-024577f407ed@googlegroups.com>

Show all headers | View raw


On 3/30/2014 10:04 PM, rivkaumiller@gmail.com wrote:
> How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead?
>
> For example, compact while loops are written like this in k&r
>
> while ((c=getchar())!=NULL){
>
>    // do certain actions
>
> }
>
> and I want to convert a stanza like
>
> fscanf ( FpSource, "%d" , &input );
> while (input != EOF){
>
>    // do certain actions
>
>    fscanf ( FpSource, "%d" , &input );
> }
>
> into the above form with getchar() so that there is a single instance of fscanf.
>
> More than one alternative would be appreciated.

     Alternative #1,pretty much what you asked for:

	while ( fscanf(FpSource, "%d", &input) , input != EOF ) {

The funny-looking thing inside the while() is an expression using
the comma operator -- yes, the comma can be an operator in C --
which evaluates its left-hand side (the fscanf() call), throws
the result away, then evaluates the right-hand side (input != EOF)
and yields the right-hand side's value as the value of the whole
expression.

     I'd recommend against using this, though: There are too many
things that can go wrong.  For starters, C promises that EOF is a
negative int, but doesn't specify any particular value.  It is
very often -1, but could be something else (a case could be made
for -129 on some systems), so you don't really *know* what number
you should enter to stop the loop.  Even if you do know the system's
EOF value (-1, say), what happens if you actually want to enter -1
as a legitimate datum?  You're out of luck.

     A worse problem occurs if fscanf() encounters "12.0" in the
input stream, or anything else that isn't strictly an int value.
On the first attempt it will digest "12", see that "." isn't a
digit and leave it alone, and store 12 in `input'.  On the next
attempt it will see the "." again, realize that it can't make
any progress, and leave things unchanged: `input' will still be
12 from the first time.  And you'll keep on getting 12 as the
value of `input' on the third, fourth, fifth, ... time until you
finally get tired and pull the plug on your program.  Similar
trouble occurs if the input is "$42" or "(17)" or a myriad of
other non-numeric stuff.

     Which leads us to Alternative #2, not what you asked for but
perhaps closer to what you should have asked for:

	while (fscanf(FpSource, "%d", &input) == 1) {

In this alternative you're not making the continue/stop decision
based on the value of the number from the input, but on whether
fscanf() was able to find a number at all.  fscanf() returns the
number of items it successfully read and stored; you're asking
for one item, so fscanf() returns 1 if and only if it was actually
able to make integer sense of its input.  In the "12.0" example
above, the first call would convert "12" to 12, store it in `input',
and return 1.  The second call would stall on the ".", leave `input'
unchanged, and return 0 -- and your program would then know that
you'd either reached the end of the input stream or encountered
something undigestible in it.

     You might be interested in telling those two cases apart,
which leads to Alternative #3:

	int status;
	...
	while ((status = fscanf(FpSource, "%d", &input)) == 1) {
	    ...
	}
	if (status == EOF) {
	    ... read all the way to the end ...
	} else {
	    ... found garbage somewhere in mid-stream ...
	}

     I might use #3 in a quick one-off program or if I had good
reason to believe the input was well-structured (e.g., the input
was the unedited output of some other program known to be well-
behaved).  For input from a less well-controlled source -- in
particular, for interactive input -- even #3 isn't robust enough
for real-world use.  I'll refer you to Question 12.20 and its
various links and footnotes on the comp.lang.c Frequently Asked
Questions (FAQ) page at <http://www.c-faq.com/> for an exposition
of some of the issues and some hints about coping with them.


-- 
Eric Sosman
esosman@comcast-dot-net.invalid

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


Thread

How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? rivkaumiller@gmail.com - 2014-03-30 19:04 -0700
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Richard Damon <Richard@Damon-Family.org> - 2014-03-30 22:31 -0400
    Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? rivkaumiller@gmail.com - 2014-03-30 19:43 -0700
      Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Richard Damon <Richard@Damon-Family.org> - 2014-03-30 23:09 -0400
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? rivkaumiller@gmail.com - 2014-03-30 19:35 -0700
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? rivkaumiller@gmail.com - 2014-03-30 19:47 -0700
    Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-03-31 11:53 +0100
      Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-31 17:50 +0000
        Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Kaz Kylheku <kaz@kylheku.com> - 2014-03-31 18:55 +0000
          Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-03-31 22:51 +0100
            Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Kaz Kylheku <kaz@kylheku.com> - 2014-03-31 22:46 +0000
              Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-04-01 00:03 +0100
              Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-31 20:33 -0400
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-04-01 09:35 +0100
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Keith Thompson <kst-u@mib.org> - 2014-04-01 07:35 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-04-01 17:19 +0100
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Keith Thompson <kst-u@mib.org> - 2014-04-01 10:14 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-04-01 18:59 +0100
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Öö Tiib <ootiib@hot.ee> - 2014-04-04 00:29 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-04-04 12:35 +0100
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Seungbeom Kim <musiphil@bawi.org> - 2014-04-08 11:48 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Öö Tiib <ootiib@hot.ee> - 2014-04-08 15:52 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-02 18:38 +0000
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Keith Thompson <kst-u@mib.org> - 2014-04-02 11:48 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-02 23:18 +0000
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Kaz Kylheku <kaz@kylheku.com> - 2014-04-03 00:05 +0000
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? James Kuyper <jameskuyper@verizon.net> - 2014-04-02 21:12 -0400
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Keith Thompson <kst-u@mib.org> - 2014-04-02 19:16 -0700
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-03 03:16 +0000
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? James Kuyper <jameskuyper@verizon.net> - 2014-04-02 15:07 -0400
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-01 23:00 +0100
                Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Seungbeom Kim <musiphil@bawi.org> - 2014-04-08 11:56 -0700
            Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-04-01 02:14 +0300
            Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-01 04:52 +0000
              Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? "BartC" <bc@freeuk.com> - 2014-04-01 09:56 +0100
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-30 22:49 -0400
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? James Kuyper <jameskuyper@verizon.net> - 2014-03-30 22:58 -0400
    Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? rivkaumiller@gmail.com - 2014-03-31 09:10 -0700
      Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? James Kuyper <jameskuyper@verizon.net> - 2014-03-31 12:25 -0400
      Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Keith Thompson <kst-u@mib.org> - 2014-03-31 10:56 -0700
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? James Kuyper <jameskuyper@verizon.net> - 2014-03-30 23:09 -0400
  Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead? Keith Thompson <kst-u@mib.org> - 2014-03-31 08:55 -0700

csiph-web