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


Groups > comp.lang.c > #25833

Re: code

From "Bill Cunningham" <nospam@nspam.invalid>
Newsgroups comp.lang.c
Subject Re: code
Date 2012-09-03 09:02 -0400
Organization Aioe.org NNTP Server
Message-ID <k229no$s4f$1@speranza.aioe.org> (permalink)
References <k215tf$b93$1@speranza.aioe.org> <0.c99f3778f5dea1cf9e21.20120903115142BST.87obln4bwh.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> There are four big things wrong here.
>
> (1) The termination condition of the loop is wrong.  It is always
> better, when using scanf and friends, to loop while the function
> reports success.  Don't loop while it isn't something you don't want,
> loop while it what you do want.  In this case, fscanf will report 1
> if it reads one number so loop while the result is one.
>
> (2) The place where you put the number read is the same place where
> you store the success/failure result from fscanf.  Even if the fscanf
> succeeded in putting a number into the location pointed to by buf,
> that number would be destroyed by putting a one into *buf (the same
> place).
>
> (3) fscanf can't do what you want (well it can if you bend the rules
> but that's not a good idea).  Its purpose is to read textual data (e.g
> sequences of digits in some text encoding like ASCII) and produce the
> internal (binary) representation that your machine uses.  That's
> exactly the opposite of what you want to do.

    I was leary as I said in another post about using fscanf and fprintf for 
this. I was playing around with some code trying to learn something and 
thought I'd post in order to learn something. Thanks for the advice. In 
dealing with binary I'll stay away from fscanf and fprintf from now on. I 
really don't know much about the *scanf family anyway.

Bill

> (4) You print a piece of uninitialised data.  *buf2 has never had
> anything put into it so the output you will get is not based on the
> input.
>
> There are some other points: using 8200 element arrays when you use
> only one element from each; writing "rt" in your second fopen call;
> printing inside the loop even if the input fails; printing again
> outside the loop and using a printf format such that you won't be
> able to interpret the output, but those are all minor compared to the
> Big Four. 

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


Thread

code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-02 22:50 -0400
  Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-02 22:53 -0400
  Re: code Barry Schwarz <schwarzb@dqel.com> - 2012-09-02 21:21 -0700
  Re: code Keith Thompson <kst-u@mib.org> - 2012-09-02 22:23 -0700
    Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 06:56 -0400
  Re: code Angel <angel+news@spamcop.net> - 2012-09-03 07:47 +0000
  Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 11:51 +0100
    Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 09:02 -0400
      Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 15:02 +0100
        Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 17:20 +0100
        Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 14:33 -0400
    Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 14:48 -0400
      Re: code Keith Thompson <kst-u@mib.org> - 2012-09-03 13:11 -0700
        Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 17:41 -0400
          Re: code Keith Thompson <kst-u@mib.org> - 2012-09-03 14:53 -0700
            Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 23:10 +0100
          Re: code "osmium" <r124c4u102@comcast.net> - 2012-09-03 16:56 -0500
            Re: code Keith Thompson <kst-u@mib.org> - 2012-09-03 18:27 -0700
        Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 17:50 -0400
          Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 17:54 -0400
            Re: code Barry Schwarz <schwarzb@dqel.com> - 2012-09-03 15:12 -0700
            Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 23:23 +0100
              Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 18:42 -0400
                Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 19:11 -0400
                Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-04 00:16 +0100
                Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 20:01 -0400
                Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-04 02:07 +0100
                Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-03 21:17 -0400
                Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-04 02:24 +0100
                Re: code Barry Schwarz <schwarzb@dqel.com> - 2012-09-03 20:55 -0700
                Re: code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-04 11:16 +0100
              Re: code Kenneth Brody <kenbrody@spamcop.net> - 2012-09-04 14:09 -0400
          Re: code Kenneth Brody <kenbrody@spamcop.net> - 2012-09-04 14:07 -0400
            Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-04 18:35 -0400
              Re: code Les Cargill <lcargill99@comcast.com> - 2012-09-04 18:49 -0500
                Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-04 20:56 -0400
                Re: code "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-04 22:05 -0400

csiph-web