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


Groups > comp.lang.c > #5944

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 18:41 -0600
Message-ID <95l4mfFmmcU1@mid.individual.net> (permalink)
References (2 earlier) <95kmvoFn2bU1@mid.individual.net> <it3apt$eb0$1@dont-email.me> <lnoc227n6w.fsf@nuthaus.mib.org> <95kt2cF48sU1@mid.individual.net> <lnfwne7ic4.fsf@nuthaus.mib.org>

Show all headers | View raw


On 06/12/2011 05:14 PM, Keith Thompson wrote:
> Uno<Uno@example.invalid>  writes:

>> I changed that.  So now I'm trying to get the data in a dynamic array:
>
> And do what with it?

Pass it to a common C extension.
>
>> $ indent -i3 hist7.c
> [...]
>> $ cat hist7.c
>> #include<stdio.h>
>> #include<limits.h>
>> #include<stdlib.h>
>> #define SIZE 20000000
>> #define SIZE2 (UCHAR_MAX+1)
>>
>> int
>> main (void)
>> {
>>      int c;
>>      long counter = 0;
>>      long counter2 = 0;
>>      long counter3 = 0;
>>      long a[SIZE2], b[SIZE2];
>>      long i, j, end;
>>      FILE *fp;
>>      char filename[] = "shoulder.wmv";
>>      unsigned char *p;
>>
>>      fp = fopen (filename, "rb+");
>>      fseek (fp, 0L, SEEK_END);
>>      end = ftell (fp);
>>      fseek (fp, 0L, SEEK_SET);
>>      printf ("file_length is %ld\n", end);
>>      p = malloc (end);
>
> You don't check whether malloc() succeeded.  (Don't bother telling
> us how much memory you have; just check whether p==NULL.)
>
> Note that the fseek() method isn't guaranteed to tell you the size
> of the file.  It probably does so on your system, but the standard
> doesn't guarantee it.
>
> (And as long as your code is non-portable, you probably might as well
> use some non-portable method like fstat().)
>
> If it works on your system, and if you're sufficiently sure that
> the file's size won't change while the program is running, that's
> probably ok.

This is my latest version that uses non-portable aspects of my platform:

[sorry, could only paste as quotation]

> $ cc -Wall -Wextra hist9.c -o hist
> hist9.c: In function ‘main’:
> hist9.c:17: warning: unused variable ‘a’
> hist9.c:15: warning: unused variable ‘counter2’
> hist9.c:14: warning: unused variable ‘counter’
> $ ./hist
> file_length is 19573712
> Main control
> b[245] is 50064
> b[246] is 38224
> b[247] is 58741
> b[248] is 92306
> b[249] is 58773
> b[250] is 65617
> b[251] is 41401
> b[252] is 97099
> b[253] is 75547
> b[254] is 113450
> b[255] is 128194
> counter3 reached 819416
> file_length is 19573712
> $ cat hist9.c
> #include <stdio.h>
> #include <limits.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <unistd.h>
>
> #define SIZE2 (UCHAR_MAX+1)
>
> int
> main (void)
> {
>    int c;
>    long counter = 0;
>    long counter2 = 0;
>    long counter3 = 0;
>    long a[SIZE2], b[SIZE2];
>    long i, j, end, size;
>    FILE *fp;
>    char filename[] = "shoulder.wmv";
>    unsigned char *p;
>    struct stat st;
>
>    fp = fopen (filename, "rb+");
>    fseek (fp, 0L, SEEK_END);
>    end = ftell (fp);
>    fseek (fp, 0L, SEEK_SET);
>    printf ("file_length is %ld\n", end);
>    p = malloc (end);
>    if (p == NULL)
>      {
> 	printf ("malloc failed.\n");
> 	exit (EXIT_FAILURE);
>      }
>    for (i = 0; i < SIZE2; ++i)
>      {
> 	b[i] = 0;
>      }
>    printf ("Main control\n");
>    for (j = 0; j < end; ++j)
>      {
> 	c = fgetc (fp);
> 	if (c != EOF)
> 	  {
> 	     p[j] = c;
> 	     ++b[c];
> 	  }
> 	else
> 	   break;
>      }
> // a little output
>    for (i = 245; i < SIZE2; ++i)
>      {
> 	printf ("b[%ld] is %ld\n", i, b[i]);
> 	counter3 = counter3 + b[i];
>      }
>    printf ("counter3 reached %ld\n", counter3);
>
> // stat
>
>    stat (filename, &st);
>    size = st.st_size;
>    printf ("file_length is %ld\n", size);
>
>    fclose (fp);
>    return 0;
> }
>
> // cc -Wall -Wextra hist9.c -o hist
> $

[snip]
>> Does the malloc'ing look good for reading this file?  Also, what do
>> POSIX and C say about the EOF character itself?  If my data are typical,
>> I would say that neither thinks that EOF is part of the file.
>
> There is no "EOF character".  fgetc() returns *either* the value
> of the character it just read (treated as an unsigned char and
> converted to int) *or* the special value EOF if it was unable to
> read a character.
>
> Typically UCHAR_MAX==255 (it could be larger) and EOF==-1 (it can
> be any negative int value).  So fgetc() will return either a value
> in the range 0..255, or the value -1.
>
> No, EOF is not part of the file; it's an indication that there's
> nothing more in the file, or that there was an error.  You can call
> feof() and/or ferror() to determine which.
>
> Do you really need to read the entire file into memory?  Perhaps you
> do, but it's often better to process the data as you're reading it.
> It depends on what your goal is.

Ok, good, that seems to square with my recollection and data.  Cheers,
-- 
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