Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Uno <Uno@example.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: looking at binary files with C |
| Date | 2011-06-12 01:41 -0600 |
| Message-ID | <95j8tqFp15U1@mid.individual.net> (permalink) |
| References | <95j3bgFj2qU1@mid.individual.net> <0ba12353-ab76-47fb-b867-1cd08cef9c4a@v11g2000prk.googlegroups.com> |
On 06/12/2011 01:05 AM, Barry Schwarz wrote:
> On Jun 11, 11:06 pm, Uno<U...@example.invalid> wrote:
>> $ indent -kr hist1.c
>> $ cc -Wall -Wextra hist1.c -o hist
>> $ ./hist
>> Segmentation fault
>> $ cat hist1.c
>> #include<stdio.h>
>> #define SIZE 100000
>>
>> int main(void)
>> {
>> int c;
>> long counter = 0;
>> long a[SIZE];
>
> If long is 4 bytes, this reserves 400,000 bytes. If 8, then 800,000.
I'm sure I have a gig of memory.
>
>> long i, j;
>> FILE *fp;
>>
>> fp = fopen("shoulder.wmv", "rb+");
>>
>> for (i = 0; i< SIZE; ++i) {
>> a[i] = 0;
>> }
>>
>> for (j = 0; j< 7000000; ++j) {
>> c = fgetc(fp);
>>
>> if (c != EOF) {
>>
>> a[counter] = c;
>
> Depending on the size of your file, this could easily exceed the
> amount of space reserved for a. When that happens, your program
> exhibits undefined behavior.
I've tried different values here, but 20 million is greater than 19 and
change.
-rw-r--r-- 1 dan dan 19573712 2011-06-05 17:32 shoulder.wmv
$ cc -Wall -Wextra hist2.c -o hist
$ ./hist
Segmentation fault
$ cat hist2.c
#include <stdio.h>
#define SIZE 20000000
int main(void)
{
int c;
long counter = 0;
long a[SIZE];
long i, j;
FILE *fp;
fp = fopen("shoulder.wmv", "rb+");
for (i = 0; i < SIZE; ++i) {
a[i] = 0;
}
for (j = 0; j < SIZE; ++j) {
c = fgetc(fp);
//printf("c is %d\n", c);
if (c != EOF) {
a[counter] = c;
counter++;
} else {
for (i = 0; i < 100; ++i) {
printf("i and frequency is %ld %ld\n", i, a[i]);
}
break;
}
}
printf("Counter reached %ld\n", counter);
fclose(fp);
return 0;
}
// cc -Wall -Wextra hist2.c -o hist
$
>
> Why are you using a long to store a character?
A long is intended to store the frequency of a given char.
>
>> counter++;
>> } else {
>> for (i = 0; i< 100; ++i) {
>> printf("i and frequency is %ld %ld\n", i, a[i]);
>
> This code does not compute any frequencies. Any non-zero a[i] has had
> its value changed exactly once and the value is set to the i-th
> character in the file.
This code hasn't done anything yet; that is true.
--
Uno
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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