Board index » cppbuilder » fread

fread


2006-03-23 12:43:55 AM
cppbuilder107
Hi all,
I am still trying to figure out some old code. What does c[4]<<1 in this
fread statement mean?
fread(buffer1,c[4]<<1,1,STM)
where:
static short *buffer1;
short c[5];
FILE* STM;
Would this be the same as: fread(buffer1,sizeof(c),1,STM) ?
Thanks in advance for your help!
Jen
 
 

Re:fread

"Jen" < XXXX@XXXXX.COM >schrieb:
Quote
Hi all,

I am still trying to figure out some old code. What does c[4]<<1 in this
fread statement mean?

fread(buffer1,c[4]<<1,1,STM)

where:
static short *buffer1;
short c[5];
FILE* STM;

Would this be the same as: fread(buffer1,sizeof(c),1,STM) ?

Thanks in advance for your help!
Jen

Would this be the same as: fread(buffer1,sizeof(c),1,STM) ?
No
Because a short contains two bytes and c[4]<<1 is the same as c[4]*2,
c[4] contains the number of shorts that should be read by this
fread()-statement.
Michael
 

Re:fread

"Jen" < XXXX@XXXXX.COM >writes:
Quote
Hi all,

I am still trying to figure out some old code. What does c[4]<<1 in this
fread statement mean?

fread(buffer1,c[4]<<1,1,STM)

where:
static short *buffer1;
short c[5];
FILE* STM;

Would this be the same as: fread(buffer1,sizeof(c),1,STM) ?
No. This code is taking the character at offset 4 in your c array,
and left shifting the bits representing that value by one. (Which is
equivalent to multiplying it by 2.) THAT is the value being passed as
the size argument to fread.
Something looks suspicious about this code. At least, it's rather
strange. Are the characters in c supposed to be length values? Make
sure that buffer1 is large enough, whatever c[4] is. Also, perhaps
you could use a well-named local variable rather than doing that
strange compuatation in the call to fread. It may help the next
person who is unfortunate enough to have to help maintain it!
--
Chris (TeamB);
 

{smallsort}

Re:fread

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >writes:
Quote
No. This code is taking the character at offset 4 in your c array,
Oops, make that "short" where it says "character". Sorry about that.
--
Chris (TeamB);
 

Re:fread

"Jen" < XXXX@XXXXX.COM >wrote in message
Quote
What does c[4]<<1 in this fread statement mean?
The value stored in c[4] is bit-shifted to the left 1 bit, which is
effectively the same as multiplying the value by 2.
Quote
Would this be the same as: fread(buffer1,sizeof(c),1,STM) ?
No. It would be the same as this:
fread(buffer1, c[4] * 2, 1, STM)
Gambit
 

Re:fread

Hi!
I'm not sure what the values in c are supposed to be. c is read from the
data file in a loop which, as far as I can tell, is never executed.
This is so frustrating! Thanks for setting me strait about the fread
statement though. I would have been way off on that one!
-Jen