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


Groups > comp.lang.c > #42373 > unrolled thread

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

Started byrivkaumiller@gmail.com
First post2014-03-30 19:04 -0700
Last post2014-03-31 08:55 -0700
Articles 2 on this page of 42 — 12 participants

Back to article view | Back to comp.lang.c


Contents

  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

Page 3 of 3 — ← Prev page 1 2 [3]


#42385

FromJames Kuyper <jameskuyper@verizon.net>
Date2014-03-30 23:09 -0400
Message-ID<lham9t$u1k$1@dont-email.me>
In reply to#42373
On 03/30/2014 10:58 PM, James Kuyper wrote:
...
> int status;
> while((status = fscanf(FpSource, "%d", &input)) != EOF)
> {
>    if(status != 1)
>    {
>       // Handle early matching failure
>    }
>    else
>    {
>       // do certain actions
>    }
> }
> if(ferror(FpSource))
> {
>     // Handle I/O error
> }
> else
> {
>     // Handle end of file
> }
...
> If you choose to simplify the code by removing all of the handling
> sections, you can drop the corresponding else blocks, and even the
> second if(). However, do NOT remove the if(status != 1) test.

I said that incorrectly. If you want to ignore early matching failures,
(it's often the case that there's nothing you can do about them), then
you could drop the if-clause, not the else-clause; but the simplest way
to re-write it would be as:

if(status == 1)
{
    // do certain actions.
}

or even more simply:

while(fscanf(FpSource, "%d", &input) == 1)
{
    // do certain actions
}

With this re-write, if you want to determine whether the loop terminated
due to an early matching failure, you'd have to check both
ferror(FpSource) and feof(FpSource), because the status isn't stored
anywhere for checking. I've used this form pretty frequently.



-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#42403

FromKeith Thompson <kst-u@mib.org>
Date2014-03-31 08:55 -0700
Message-ID<lnwqfagy6m.fsf@nuthaus.mib.org>
In reply to#42373
rivkaumiller@gmail.com writes:
> 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
>
> }

NULL is a null pointer constant.  getchar() returns EOF when it's unable
to read a character.  And be sure that "c" is defined as an int, not as
a char.

> 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.

To use any input function, you need to understand the value that it
returns.

getchar() returns the value of the character it just read from stdin,
treated as an unsigned char and converted to int (for typically systems,
it will be a value in the range 0..255).  That's if it's able to read a
character.  If it's not, either because there are no more characters to
be read or because there was an error, it returns EOF, which is a
negative int value (typically -1, but don't assume that).

fscanf(), if it's successful, returns the number of items it read.
For

    fscanf(FpSource, "%d" , &input);

it returns 1 if it was able to read an int value into "input".

If an input failure occurs, it returns EOF.  Otherwise, it returns the
number of items it was able to read, which in this case will be either 0
or 1.  (It's 0 if the input doesn't match the syntax of an optionally
signed decimal integer.)

That, along with the other followups, should be enough information get
you going.

Note that comparing the value of input to EOF doesn't make much sense;
input==EOF doesn't say anything meaningful about the input.

-- 
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"

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web