Sunday, June 12, 2011

comp.lang.c - 3 new messages in 2 topics - digest

comp.lang.c
http://groups.google.com/group/comp.lang.c?hl=en

comp.lang.c@googlegroups.com

Today's topics:

* bdiff - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c/t/cd7948c209a90226?hl=en
* looking at binary files with C - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/b032972b5c8e3ac9?hl=en

==============================================================================
TOPIC: bdiff
http://groups.google.com/group/comp.lang.c/t/cd7948c209a90226?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Jun 11 2011 11:47 pm
From: Uno


On 06/09/2011 09:58 PM, Morris Keesan wrote:
> On Fri, 10 Jun 2011 00:11:37 -0400, Uno <merrilljensen@q.com> wrote:
>
>
>> I don't have an appropriate reference for fgetc and dinkumware has
>> been down.
>>
>> What would I need to install to be able to man fgetc?
>
> Try
> http://pubs.opengroup.org/onlinepubs/009695399/functions/fgetc.html
>

Thx, Morris.

If the end-of-file indicator for the input stream pointed to by stream
is not set and a next byte is present, the fgetc() function shall obtain
the next byte as an unsigned char converted to an int, from the input
stream pointed to by stream, and advance the associated file position
indicator for the stream (if defined). Since fgetc() operates on bytes,
reading a character consisting of multiple bytes (or "a multi-byte
character") may require multiple calls to fgetc().


What is the relationship between unsigned chars and ints? Both unsigned
and signed chars are subsets of ints, but why are unsigned chars the
representation of choice with binary data?
--
Uno

==============================================================================
TOPIC: looking at binary files with C
http://groups.google.com/group/comp.lang.c/t/b032972b5c8e3ac9?hl=en
==============================================================================

== 1 of 2 ==
Date: Sat, Jun 11 2011 11:58 pm
From: Uno


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

== 2 of 2 ==
Date: Sun, Jun 12 2011 12:05 am
From: Barry Schwarz


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

==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.c"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c?hl=en

To unsubscribe from this group, send email to comp.lang.c+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate