The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
read a file bit-by-bit
Discuss read a file bit-by-bit in the C Programming forum on Dev Shed. read a file bit-by-bit C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 30th, 2004, 06:31 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Constant Limbo
|
|
|
read a file bit-by-bit
Is there a way to read a file bit-by-bit?
what i want exactly is to read in 11 bits
at a time, manipulate the data based
on the 11 bits.
Note: the 11 is an arbitrary number.
That value may be any number 1 to 255.
Its more the function i am looking for, not
the number of bits.
thanks in advance.
|

January 30th, 2004, 07:10 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|
No -- the smallest unit that can be read or written is one BYTE -- 8 bits.
|

January 30th, 2004, 07:36 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 68
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
>one BYTE -- 8 bits
8 bits is an octet, a byte isn't necessarily 8 bits so you shouldn't rely on it.
|

January 30th, 2004, 08:17 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|

January 30th, 2004, 08:34 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Canada
Posts: 124
Time spent in forums: 21 m 28 sec
Reputation Power: 10
|
|
fread() will let you specify the number of char's to read in (a char will typically be 8bits). You need to allocate storage space then pass in a pointer to the space. Just note that many small reads take the same or more time than one huge read.
I'm sure someone can post a istream way to do this, but I trust streams as far as I can throw them 
|

January 30th, 2004, 08:54 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|
I don't like streams either -- formatting capability is really terrible compared to printf(). If you use VC++ 6.0 compiler and step into any of the stream functions you will find that they are really implemented by using FILE structure and associated functions. But those functions are a whole lot easier to use than streams. streams were very poorly designed in my opinion.
|

January 30th, 2004, 09:37 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Constant Limbo
|
|
ok
following on that theme
(not all chars are 8-bit bytes)
if there are differences how does the
fread -- or equivalent -- know how many
bits to travel to get to the next address?
if i knew this, it might be easier to
write a similar function
(or modify the existing one)
to jump any number of bits i declare.
yes?
so,
now for my next question.
does anyone know how this is accomplished?
(determining bit jumps, not overloading a function  )
|

January 30th, 2004, 09:52 PM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
I suggest you have a class that handles the file IO and buffers your bit requests. You can only manipulate in 8, 16, or 32 bit values (64 if you happen to have a 64 bit machine and OS) so you can't request 11 bits. You can request 16 and can mask off the other bits or code so that you ignore them. You will have to do some math to keep track of where your bit marker is and do bit shifting to get the alignment proper for each read, but it shouldn't be too difficult.
A word of caution: stick with unsigned integral values. Signed values behave differently when you do bit shifting and though a double will provide 8 64 bits of space, you may have problems with accessing them sometimes.
|

January 30th, 2004, 10:15 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
Quote: Originally posted by L7Sqr
ok
following on that theme
(not all chars are 8-bit bytes)
if there are differences how does the
fread -- or equivalent -- know how many
bits to travel to get to the next address?
if i knew this, it might be easier to
write a similar function
(or modify the existing one)
to jump any number of bits i declare.
yes?
so,
now for my next question.
does anyone know how this is accomplished?
(determining bit jumps, not overloading a function ) |
you can only do that on computers that allow it -- unix, MS-DOS and Windows are all 8-bit operating systems. There are, of course, hundreds of other operating systems, and some of them may not use the 8-bit scheme. On those, I suspece fread() operates the same because the compiler manufacturer implemented it according to the operating system specs.
In unix, lines, ms-dos and all versions of Windows, there is no way to skip a specified number of bits -- that's impossible.
|

January 31st, 2004, 12:47 AM
|
 |
Left due to despotic ad-min
|
|
Join Date: Jun 2003
Posts: 1,044
  
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 12
|
|
Use caching. The standard mechanisms for reading a file read data a byte at a time, or in groups of multiple bytes.
The following UNTESTED example assumes bytes are 8 bits. It should be enough to get you started.
PHP Code:
typedef struct _BitData
{
FILE *file;
int last_byte;
int bit_count;
} BitData;
/* The following assumes the argument is initialised with a
valid FILE handle (eg returned from fopen), with bit_count
set to EOF. The function returns EOF if EOF of the file has
been read.
*/
int GetBit(BitData *f)
{
int temp;
if (f->bit_count == 0)
{
f->last_byte = getc(f->file);
}
if (f->last_byte == EOF) return EOF;
temp = (f->last_byte & (1 << (7 - f->bit_count)) != 0; /* get next bit */
++f->bit_count;
if (f->bit_count == 7) f->bit_count = 0;
return temp;
}
If you think about it, the standard file read functions do this on a larger scale. Internally, they read blocks (eg 512 or more) bytes from a file, and then return one byte at a time from the block they have read.
|

January 31st, 2004, 09:21 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Constant Limbo
|
|
|
thank you.
most helpful.
this is more or less what i was after.
(although it would have been nice
to have a built-in function )
thanks for the help all.
|

January 31st, 2004, 05:37 PM
|
 |
Left due to despotic ad-min
|
|
Join Date: Jun 2003
Posts: 1,044
  
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 12
|
|
|
It might have been nice to have had a built in function, but there is no such beast.
Wanting data a bit at a time is not the most common usage, whereas reading a byte (or a group of bytes) is. So that's what the standard library supports. Heck, there need to be some things left for the humble programmer to do :-)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|