[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

looking at binary files with C

Uno

6/12/2011 6:07:00 AM

$ 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];
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;
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 hist1.c -o hist
$

So, why the seg fault?
--
Uno
50 Answers

Ian Collins

6/12/2011 6:42:00 AM

0

On 06/12/11 06:06 PM, Uno 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];
> long i, j;
> FILE *fp;
>
> fp = fopen("shoulder.wmv", "rb+");

Checking for success here would be a good idea.

--
Ian Collins

Uno

6/12/2011 6:58:00 AM

0

On 06/12/2011 12:41 AM, Ian Collins wrote:
> On 06/12/11 06:06 PM, Uno 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];
>> long i, j;
>> FILE *fp;
>>
>> fp = fopen("shoulder.wmv", "rb+");
>
> Checking for success here would be a good idea.
>

Data's coming in:

c is 27
c is 25
c is 157
c is 241
c is 16
c is 149
c is 242
c is 192
c is 75
c is 49
c is 40
c is 108
c is 28
c is 76
c is 171
c is 60
c is 174
c is 233
c is 68
c is 50
c is 93
c is 152
c is 240
c is 2
c is 11
c is 172
c is 69
c is 76
c is 189
c is 129
c is 132^C
$ cat hist2.c
#include <stdio.h>
#define SIZE 100000

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 < 7000000; ++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
$
--
Uno

Barry Schwarz

6/12/2011 7:05:00 AM

0

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.

>      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.

Why are you using a long to store a character?

>             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.

>             }
>
>             break;
>         }
>      }
>      printf("Counter reached %ld\n", counter);
>      fclose(fp);
>      return 0;
>
> }
>
> // cc -Wall -Wextra hist1.c -o hist
> $
>
> So, why the seg fault?
> --
> Uno

Uno

6/12/2011 7:42:00 AM

0

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

Joachim Schmitz

6/12/2011 9:45:00 AM

0

Uno wrote:
> 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.

But you're not using it for long a[SIZE], so accessing a[SIZE+1] would
result in a SIGSEGV usually.

>>
>>> 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.

Have you tried using a debugger on this? Where does the debugger report the
program to fail?
You probably want to add -g to the compiler options.

Bye, Jojo


Ike Naar

6/12/2011 10:27:00 AM

0

On 2011-06-12, Uno <Uno@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];
> 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;
> 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 hist1.c -o hist

Don't repeat yourself.

> $
>
> So, why the seg fault?

Is shoulder.wmv more than SIZE bytes long?

Eric Sosman

6/12/2011 12:24:00 PM

0

On 6/12/2011 2:06 AM, Uno wrote:

> #define SIZE 100000
> [...]
> long a[SIZE];
> [...]
> for (j = 0; j < 7000000; ++j) {
> [...]
> So, why the seg fault?

Choose the phrase that most accurately completes this statement:
"7000000 is ________ 100000."

a) less than
b) greater than
c) equal to
d) more equal to
e) more tweeted than

--
Eric Sosman
esosman@ieee-dot-org.invalid

Bartc

6/12/2011 1:27:00 PM

0

"Uno" <Uno@example.invalid> wrote in message
news:95j3bgFj2qU1@mid.individual.net...

> c = fgetc(fp);
>
>
> if (c != EOF) {
>
> a[counter] = c;

Try:
++a[c];

here if trying to count frequency of each character.

Up above, you might need something like:

long a[256]={0};

--
Bartc

Joe Pfeiffer

6/12/2011 4:25:00 PM

0

Uno <Uno@example.invalid> writes:
> So, why the seg fault?

Let's just say this code is a prime example of why I took points of on
student programs that had magic numbers (like 7000000 for instance) in
the executable code.

Joe Pfeiffer

6/12/2011 4:26:00 PM

0

Uno <Uno@example.invalid> writes:

> 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.

That doesn't mean your array is a gig long; your array is only as long
as you made it.