473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading 32bits from a file

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.
Nov 13 '05 #1
6 2035

"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
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
Nov 13 '05 #2
Mac
On Sat, 27 Sep 2003 11:27:08 +0000, Mantorok Redgormor wrote:
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
--
Nov 13 '05 #3
"Mac" <fo*@bar.net> wrote in message
news:pa****************************@bar.net...
On Sat, 27 Sep 2003 11:27:08 +0000, Mantorok Redgormor wrote:
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.


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.

--
Peter
Nov 13 '05 #4
Mantorok Redgormor wrote:

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.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5
Mantorok Redgormor <ne*****@tokyo.com> wrote:
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.

Nov 13 '05 #6

"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message

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.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
9
by: VBS Programer | last post by:
I am working with a thermal printer that returns it's status in a string. In vb6.0 I would use the MidB,AscB functions to examine the first 32 bits. What can I use in vb.net to do the same.
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
3
by: roaher | last post by:
hi I have some trouble reading this macro: #define SMC_inl(r) (*((volatile dword *)(SMC_BASE_ADDRESS+(r)))) also consider that SMC_BASE_ADDRESS is address base of I/O mapped peripherial...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
9
by: Noel Milton | last post by:
Hi: Ok, I've just read in up to 65,535 bytes into a char array (using the recvfrom socket API call). So I have an array of 8 bit char's (char recvString;). Now, four (4) consecutive bytes...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.