reading 32bits from a file

M

Mantorok Redgormor

Would a portable method of handling 32 bits of data from a file, be to
use something like unsigned long(I need to read in 32-bits of data)?
or is there some smaller type that guarantees at least 32 bits of
value bits?

Also, off topic but does anyone know how to count all frames from an
mp3 file? If each frame size is always the same then that would be
easier and I could just read in all records of a fixed size. I believe
this is how the length of the mp3 is computed.
 
M

Mike Wahler

Mantorok Redgormor said:
Would a portable method of handling 32 bits of data from a file,

You cannot portable read or write *exactly 32 bits*, no.
You can read or write the number of characters the sum
of whose bits is equal to *or greater* than 32.

be to
use something like unsigned long(I need to read in 32-bits of data)?

Unsigned long must have a minimum size of 32 bits, but is
allowed to be larger.
or is there some smaller type that guarantees at least 32 bits of
value bits?

No. 'long' and 'unsigned long' do have this guarantee.
'int' and 'unsigned int' might be this size or larger,
but are not required to.
Also, off topic but does anyone know how to count all frames from an
mp3 file?

Some people here probably do, but those that care about
the group will not answer that here. Try asking where
its topical.
If each frame size is always the same then that would be
easier and I could just read in all records of a fixed size. I believe
this is how the length of the mp3 is computed.

Why not look it up and/or ask in a group where it's topical?

-Mike
 
M

Mac

Would a portable method of handling 32 bits of data from a file, be to
use something like unsigned long(I need to read in 32-bits of data)?
or is there some smaller type that guarantees at least 32 bits of
value bits?

Also, off topic but does anyone know how to count all frames from an
mp3 file? If each frame size is always the same then that would be
easier and I could just read in all records of a fixed size. I believe
this is how the length of the mp3 is computed.

There is no guarantee that unsinged long is 32 bits. AFAIK there is no
guarantee that any 32-bit type exists.

IIRC, C99 may have some way of finding a type that's exactly 32 bits, proivided
that such a type exists.

On the other hand, unsigned chars are usually 8 bits, and you can check by
inspecting CHAR_BIT. (CHAR_BIT is in limits.h)

So you could verify that CHAR_BIT is a divisor of 32, then declare an
array of unsigned chars of lenght 32/CHAR_BIT to store your 32 bits.

HTH

Mac
--
 
P

Peter Nilsson

Mac said:
There is no guarantee that unsinged long is 32 bits.

As Mike said, there is a guarantee that it will be at least 32-bits.
AFAIK there is no
guarantee that any 32-bit type exists.

IIRC, C99 may have some way of finding a type that's exactly 32 bits, proivided
that such a type exists.

Only if the implementation of that type is unpadded two's complement.
 
J

Joe Wright

Mantorok said:
Would a portable method of handling 32 bits of data from a file, be to
use something like unsigned long(I need to read in 32-bits of data)?
or is there some smaller type that guarantees at least 32 bits of
value bits?
Mantorok, you have been here (in clc) for more than a day now and I
suspect you already know the answer. You can fwrite() 32 bits to a file
and fread() the same 32 bits back, can't you? What exactly do you mean
by 'portable'? C guarantees that long is at least 32 bits wide. It could
be wider on one machine than another. It may be big endian on one
machine and little endian on the other. Binary portability via file I/O
is not a given.
Also, off topic but does anyone know how to count all frames from an
mp3 file? If each frame size is always the same then that would be
easier and I could just read in all records of a fixed size. I believe
this is how the length of the mp3 is computed.

I'll betcha lots of people know how to do that. They don't live here.
 
K

Kevin Easton

Mantorok Redgormor said:
Would a portable method of handling 32 bits of data from a file, be to
use something like unsigned long(I need to read in 32-bits of data)?
or is there some smaller type that guarantees at least 32 bits of
value bits?

unsigned long is the lowest-rank type guaranteed to have at least 32
value bits, yes. To read in exactly 32 bits from a file, you can read 4
unsigned chars and pack the lowest 8 bits in them into an unsigned long
in some defined manner. Then you just mandate that the 32 bits in the
file must be stored in the lowest 8 bits of 4 consecutive characters -
and it's up to the user of your program on a platform with CHAR_BIT > 8
to ensure the file has the correct structure :)

- Kevin.
 
M

Malcolm

Mantorok Redgormor said:
Would a portable method of handling 32 bits of data from a file, be to
use something like unsigned long(I need to read in 32-bits of data)?
or is there some smaller type that guarantees at least 32 bits of
value bits?
Write two functions

void put32(FILE *fp, long x)

and

long get32(FILE *fp)

You might also want unsigned versions. Make them store in big- or
little-endian format depending on taste, by using the shift and logical
operators to mask of 8 bits at a time.
This gives you all the portability you reasonably need. On a machine with
non-8-bit chars there will certainly be some way to read a binary file
generated on a machine with 8-bit bytes, though you might have to make some
tweaks to your program to port it.
Also, off topic but does anyone know how to count all frames from an
mp3 file? If each frame size is always the same then that would be
easier and I could just read in all records of a fixed size. I believe
this is how the length of the mp3 is computed.
You need to look up the mp3 format. Since it is a compression scheme,
requiring every frame to be of exactly the same length would reduce
efficency considerably. However it is designed to be broadcast at a constant
bit rate, so presumably the solution is to insert padding every few frames.
I don't actually know, which is why clc isn't a particularly good forum for
questions about general programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,588
Members
47,209
Latest member
Ingeborg61

Latest Threads

Top