473,775 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2058

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
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.ne t
"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
3066
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
2040
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
10377
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
4
9842
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 = tmpfile(); /* write into tmpFile */ ...
3
1836
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 registers, r is register offset. Does that read: return 32bits of data stored in memory location SMC_BASE_ADDRESS+r.
6
6372
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
7
6063
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
9
2386
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 somewhere within that array represent a counter. How can I take those 4 individual bytes, concatinate the 32bits that
10
5615
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10270
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6718
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5361
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4018
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2854
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.