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


Groups > comp.lang.c > #42406

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

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: How to modify ((c=getchar())!=NULL) to use fscanf or scanf instead?
Date 2014-03-31 12:25 -0400
Organization Self
Message-ID <533996EF.2090609@verizon.net> (permalink)
References <c6584a57-8f78-4974-bd65-024577f407ed@googlegroups.com> <lhaljr$pdn$1@dont-email.me> <fa235e5a-0578-4faa-9820-da08ac73674c@googlegroups.com>

Show all headers | View raw


On 03/31/2014 12:10 PM, rivkaumiller@gmail.com wrote:
> On Sunday, March 30, 2014 7:58:03 PM UTC-7, James Kuyper wrote:
...
>> int status;
>> while((status = fscanf(FpSource, "%d", &input)) != EOF)
>> {
>>    if(status != 1)
>>    {
>>       // Handle early matching failure
>>    }
...
> "status" is a good self-commenting alias for the return value of fscanf().
> 
> I finally found this worked well
> 
> while (fscanf ( FpSource, "%d" , &input ) != EOF){
>   // do certain actions with "input"
> }
> 
> as all it cares about is reaching the end of the input file.

And it therefore does not address this issue:

>> 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. If status
>> is not equal to 1, then the value of 'input' is indeterminate. That
>> means it can be either an unspecified integer, or a trap
>> representations. Any attempt to use a trap representation has undefined
>> behavior, but very few implementations support 'int' types with trap
>> representations. Much more important is the fact that the value of
>> 'input' is unspecified. That doesn't mean that it's random - it could be
>> highly predictable if you know enough about that particular
>> implementation of the C standard library. However, it does mean that
>> there's no guarantees about which value it has. There's nothing useful
>> your program can portably do with that value, so it should not "do
>> certain actions" using that value.

Are you certain that your input stream won't have any early matching
errors? Such certainty is often a matter of self-delusion.

Or do you simply not care about the possibility that your program might
end up processing meaningless values that have nothing to do with the
contents of your input file? If that's not a problem, you can simplify
your program a great deal by not even bothering to read that file.

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