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


Groups > comp.lang.c > #5930

Re: looking at binary files with C

From Uno <Uno@example.invalid>
Newsgroups comp.lang.c
Subject Re: looking at binary files with C
Date 2011-06-12 17:01 -0600
Message-ID <95kur4Fg95U1@mid.individual.net> (permalink)
References <95j3bgFj2qU1@mid.individual.net> <1bk4crnhkt.fsf@snowball.wb.pfeifferfamily.net> <95kmvoFn2bU1@mid.individual.net> <it3bsv$kvr$1@dont-email.me>

Show all headers | View raw


On 06/12/2011 03:42 PM, Eric Sosman wrote:
> On 6/12/2011 4:47 PM, Uno wrote:
>>
>> There were a lot of problems with this source that needing ironing out,
>> and I needed to start to somehwhere.
>
> An *excellent* place to start is with a description of what
> you want the program to do. As it is, you've just thrown a bunch
> of code at us and expected us to divine your intentions. If the
> code were not so clearly broken, divination might stand a chance;
> even then it would be better to state your purpose. With broken
> code, such a statement is even more important.

It takes me a few posts before I get a syntactical C program to use to 
describe what I'm doing.  Without referent source, I can't communicate this.
>
>> When I've got a program that
>> compiles, I like to have the magic numbers #defined.
>
> Your choice, but you're making extra work for yourself. When
> you're replacing numeric constants with macro names and you come
> across a 3, should you change it to SHIFTS_PER_DAY or to VERSION?
> Essentially, you've created a reverse-engineering problem for yourself,
> giving yourself an extra opportunity to make misteaks.

But you did notice that I have
#define SIZE2 (UCHAR_MAX+1)
, and this was the seg fault I couldn't get my head around when I first 
posted.

>
>> I haven't really
>> done much C on a 64-bit processor, so my ballparking seems to be wanting.
>
> Nothing in your code stands out as bitness-related.

A client paid me to put a binary file on the net, and I used perl's 
net::ftp without knowing that I had to call the binary method in order 
to upload without *nix eating ascii 13.  (Thx a lot c.l.p.misc: for 
nothing.)

I knew by comparing what I uploaded to what I downloaded, I would find 
the problem.  Others, for example, Alan Curry, might claim that *they* 
found the problem, and I do not dispute that.
>
>> #include <stdio.h>
>> #include <limits.h>
>> #define SIZE 20000000
>> #define SIZE2 (UCHAR_MAX+1)
>>
>> int main(void)
>> {
>> int c;
>> long counter = 0;
>> long counter2 = 0;
>> long a[SIZE2];
>> long i, j;
>> FILE *fp;
>> fp = fopen("shoulder.wmv", "rb+");
>
> Why the "+", since you never write to the file? And why
> is there still no check for failure, after others have already
> pointed out the lack?

The + on all opens because I might want to write to them.  No check for 
failure because the only one I've got in my head is
or die "death $@\n";  Is there a K&R2 reference for how to do it?
>
>> printf("UCHAR_MAX + 1 is %d\n", SIZE2);
>> for (i = 0; i < (SIZE2); ++i) {
>
> The parentheses around SIZE2 are harmless, but not helpful:
> They're just unnecessary clutter that gives the reader more to
> parse. Perhaps you think they're necessary because SIZE2 expands
> to an expression rather than to a single token? But the expression
> is already parenthesized (as good macros should be), so tacking on
> an extra layer isn't needed. What you've written is equivalent to
>
> for (i = 0; i < ((UCHAR_MAX+1)); ++i)
>
>> a[i] = 0;
>> }
>> for (j = 0; j < SIZE; ++j) {
>> c = fgetc(fp);
>> counter++;
>> if (c != EOF) {
>> a[c] = ++a[c];
>
> Undefined behavior: Two modifications of the same object
> with no intervening sequence point.

Fixed that.  Thx.
>
>> } else
>> break;
>> }
>> for (i = 0; i < (SIZE2); ++i) {
>> printf("a[%ld] is %ld\n", i, a[i]);
>> counter2 = counter2 + a[i];
>> }
>> printf("Counter reached %ld\n", counter);
>> printf("Counter2 reached %ld\n", counter2);
>> fclose(fp);
>> return 0;
>> }
>>
>> // cc -Wall -Wextra -g hist3.c -o hist
>> $
>>
>> I needed two different SIZES, one that would be larger than the byte
>> count of the file to be read and another that was one greater than
>> UCHAR_MAX.
>
> SIZE2 (or equivalent) is necessary, yes. But SIZE seems to
> serve no useful purpose -- unless, as mentioned above, you have
> purposes that aren't stated.

Right, so I'm looking for your comment on the dynamic allocation which I 
posted as a reply to keith.
>
>> Seems to behave.
>
> "Things are seldom what they seem.
> Skim milk masquerades as cream ..."
>

Arizona on fire.
Brewer raises my ire.
Of C I don't tire.
-- 
Uno

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


Thread

looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 00:06 -0600
  Re: looking at binary files with C Ian Collins <ian-news@hotmail.com> - 2011-06-12 18:41 +1200
    Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 00:58 -0600
  Re: looking at binary files with C Barry Schwarz <schwarz45@yahoo.com> - 2011-06-12 00:05 -0700
    Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 01:41 -0600
      Re: looking at binary files with C "Joachim Schmitz" <jojo.nospam@schmitz-digital.de> - 2011-06-12 11:44 +0200
      Re: looking at binary files with C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-12 10:25 -0600
  Re: looking at binary files with C Ike Naar <ike@sverige.freeshell.org> - 2011-06-12 10:26 +0000
  Re: looking at binary files with C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-12 08:23 -0400
    Re: looking at binary files with C Hans Vlems <hvlems@freenet.de> - 2011-06-13 23:48 -0700
    Re: looking at binary files with C Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-23 00:55 -0700
  Re: looking at binary files with C "BartC" <bc@freeuk.com> - 2011-06-12 14:26 +0100
  Re: looking at binary files with C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-12 10:24 -0600
    Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 14:47 -0600
      Re: looking at binary files with C "BartC" <bc@freeuk.com> - 2011-06-12 22:23 +0100
        Re: looking at binary files with C Keith Thompson <kst-u@mib.org> - 2011-06-12 14:29 -0700
          Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 16:31 -0600
            Re: looking at binary files with C Keith Thompson <kst-u@mib.org> - 2011-06-12 16:14 -0700
              Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 18:41 -0600
      Re: looking at binary files with C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-12 17:42 -0400
        Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 17:01 -0600
          Re: looking at binary files with C Keith Thompson <kst-u@mib.org> - 2011-06-12 16:23 -0700
            Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 17:24 -0600
              Re: looking at binary files with C Ian Collins <ian-news@hotmail.com> - 2011-06-13 11:28 +1200
                Re: looking at binary files with C Geoff <geoff@invalid.invalid> - 2011-06-12 16:52 -0700
              Re: looking at binary files with C Keith Thompson <kst-u@mib.org> - 2011-06-13 08:51 -0700
            Re: looking at binary files with C Seebs <usenet-nospam@seebs.net> - 2011-06-13 14:39 +0000
              Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-13 20:01 -0600
                Re: looking at binary files with C Keith Thompson <kst-u@mib.org> - 2011-06-13 19:24 -0700
                Re: looking at binary files with C Seebs <usenet-nospam@seebs.net> - 2011-06-14 17:02 +0000
                Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-16 01:32 -0600
          Re: looking at binary files with C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-13 07:57 -0400
            Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-13 20:23 -0600
              Re: looking at binary files with C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-13 23:12 -0400
                Re: looking at binary files with C lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2011-06-14 01:09 -0700
          Re: looking at binary files with C Oliver Jackson <chexmofo@hotmail.com> - 2011-06-15 20:15 -0700
      Re: looking at binary files with C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-12 18:41 -0600
      Re: looking at binary files with C James Waldby <not@valid.invalid> - 2011-06-13 01:16 +0000
        Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-12 19:48 -0600
        Re: looking at binary files with C "io_x" <a@b.c.invalid> - 2011-06-14 12:19 +0200
          Re: looking at binary files with C lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2011-06-14 03:58 -0700
            Re: looking at binary files with C "io_x" <a@b.c.invalid> - 2011-06-14 19:07 +0200
              Re: looking at binary files with C "io_x" <a@b.c.invalid> - 2011-06-14 19:46 +0200
              Re: looking at binary files with C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-14 16:08 -0600
          Re: looking at binary files with C Ian Collins <ian-news@hotmail.com> - 2011-06-15 08:47 +1200
      Re: looking at binary files with C Hans Vlems <hvlems@freenet.de> - 2011-06-13 23:51 -0700
  Re: looking at binary files with C Michael Press <rubrum@pacbell.net> - 2011-06-15 13:06 -0700
    Re: looking at binary files with C Uno <Uno@example.invalid> - 2011-06-16 01:38 -0600
      Re: looking at binary files with C Michael Press <rubrum@pacbell.net> - 2011-06-16 00:54 -0700
        Re: looking at binary files with C James Waldby <not@valid.invalid> - 2011-06-16 16:59 +0000
      Re: looking at binary files with C "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-16 18:32 +0100

csiph-web