-
November 20th, 2006, 01:48 PM
#1
reading bitwise values fopen(..) fread(..)
Hi All,
I'm currently doing this code for a flash swf file format, and it deals a lot with bit by bit data....so for example the header format would be:
F,W,S [char]
version [unsigned int]
x bits for sizes A,B,C,D
A,B,C,D of bit size x (from line above)
So as I read byte by byte I was wondering if there is a tidy way of reading data on the per bit data...or as I'm thinking of doing it is reading the whole lot into a single buffer and use lots of shift operators to sort things...but it seems a bit messy and wondered if there was any good ideas or example using the standard librarys.
Thanks,
Ben
-
November 20th, 2006, 03:44 PM
#2
Re: reading bitwise values fopen(..) fread(..)
Best way to do this is to define a structure and read the entire block of data in. Then you can twiddle with the bits as you see fit...
The benifits of this is
(1) it isolates your file reading and your header logic
(2) it minimizes your disk access which is the slowest part of any computer program
(3) it's modular, you can reuse the code to deal with any file header, just by changing the header structure...
Design considerations...
In order to use this powerful technique for reading formated data you need to know two things..
1) the header and subheader structure definitions
2) the number and order of the header and any substructures
usually the header will tell you how many subheaders are following..
in your case "the number of bits" field usually isn't the actual number of bits rather it's the number of bits used. The header will contain placeholders for 8, 16, 32, 64 bits and will only use as many as specified in this field.. thus you can use an unsigned char, short, long, or DWORD to actually contain your databits...
Hope this makes sense...
Code:
typedef _MYHEADER
{
char chTop;
unsigned int uiVersion;
unsigned int uiNumOfBits;
unsigned int uiDataBits; // 32 bits will be read
} MYHEADER, *PMYHEADER;
main()
{
MYHEADER myHeader;
stream = fopen( "myfile.txt", r );
fread( stream, &myHeader, sizeof( MYHEADER));
fclose( steam );
}
Last edited by JMS; November 20th, 2006 at 03:54 PM.
-
November 21st, 2006, 07:54 AM
#3
Re: reading bitwise values fopen(..) fread(..)
Maybe std::bitset can be useful?
Regards,
Guido
- Guido
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|