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


Groups > comp.lang.c > #25799

Re: code

From Barry Schwarz <schwarzb@dqel.com>
Newsgroups comp.lang.c
Subject Re: code
Date 2012-09-02 21:21 -0700
Organization A noiseless patient Spider
Message-ID <tha84896ljr6c610103d169ced1799rn87@4ax.com> (permalink)
References <k215tf$b93$1@speranza.aioe.org>

Show all headers | View raw


On Sun, 2 Sep 2012 22:50:37 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>    I worked and worked on this code until my compiler gave no more errors 
>even with -Wall -ansi turned on. So I think it's valid. The question is is 

Think again.

>it doing what I want it to. I don't think so. Its never stopped and I've had 

See, upon reflection you can reach a correct conclusion.

>to interrupt it while running. I get a bunch of textual 00 all across the 
>screen. The idea is to take a binary and convert it to text and print it in 

Describe in detail what the input binary looks like.  

>hex. I never get out of the do-while loop. Like a hex dump does but a hex 

If you want to print it in hex, what is the %i doing in your print
format?

>dump doesn't convert a binary into text. Is what I'm wanting valid? Am I 

Provide a short sample of input and what you want the output to look
like.

>getting what I'm asking for. Here's the code if your reader lets you read it 
>neatly.
>
>Bill
>#include <stdio.h>
>#include <stdlib.h>
>
>int main(void)
>{
>    int buf[8200];
>    unsigned buf2[8200];

Where did these array sizes come from?  What purpose did you want buf2
to serve?

>    FILE *fp, *fp2;
>    if ((fp = fopen("6", "rb")) == NULL) {
> perror("fopen 1");
> exit(1);
>    }
>    if ((fp2 = fopen("b", "wt")) == NULL) {
> perror("fopen 2");
> exit(1);
>    }
>    do {
> *buf = fscanf(fp, "%i", buf);

This statement says
    Read a bunch of text characters from a stream and convert them to
an int value.  (I have no idea if the fact that you opened the file as
binary will have any effect on this operation.)  Store the int value
in the int object pointed to by buf.  (Since buf is an array, this
expression is converted to &buf[0] which is a perfectly good int*.)
   After this completes, store the number of successful conversions in
*buf (which is the same as buf[0].  So you completely erase the value
that fscanf obtained and proceed to store either 0, 1, or EOF in
buf[0].

Under the very risky assumption that the call to fscanf had a real
purpose, I submit to you that this code does not accomplish what you
want.  It is now up to you to tell us in PRECISE DETAIL what you
intended this statement to accomplish.

By the way, you should always check that fscanf succeeded.

> fprintf(fp2, "%i %x", *buf, *buf2);

Since buf2 was never initialized with any values, all the elements of
the array are indeterminate.  The act of evaluating indeterminate
values results in undefined behavior.

Since you don't specify any whitespace after the %x or before the %i,
whatever value is written for the %i for iteration n+1 will
immediately abut the value previously written for the %x on iteration
n.  Something like
     1 0x123456781 0x12345678...

>    }

It's a shame you have reneged on your commitment to indent your code.

>    while (*buf != EOF);
>    fclose(fp);
>    fclose(fp2);
>    printf(" %i %u", *buf, *buf2);

All of buf2 is still indeterminate so this is also undefined.

>    return 0;
>}
>

-- 
Remove del for email

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