views:

84

answers:

4
ifstream inStream;
inStream.open(filename.c_str(), fstream::binary);
if(inStream.fail()){
   cout<<" Error in opening file: "<<filename;
   exit(1);
}

Let's say we just want to deal with individual bits from the file. I know we can read the file char by char, but can we read it bit by bit just as easily?

A: 

of course you CANNOT read bits!

luca
+1  A: 

Files are typically read in units that are greater than a bit (usually a byte or above). A single bit file would still take at least a whole byte (actually, it would take multiple bytes on disk based on the file system, but the length could be determined in bytes).

However, you could write a wrapper around the stream that provides the next bit every time, while internally reading a character, supplying bits whenever asked, and reading the next character from the file when there is a request that could not longer be filled from the previous character. I assume that you know how to turn a single byte (or char) into a sequence of bits.

Since this is homework, you are probably expected to write this yourself instead of using an existing library.

Uri
+1  A: 

You'll have to read from the file byte by byte and then extract bits as needed from the read byte. There is no way to do IO at bit level.

I guess your binary file is the huffman encoded and compressed file. You'll have to read this file byte by byte, then extract bits from these bytes using bitwise operators like:

char byte;
// read byte from file.
unsigned char mask = 0x80; // mask for bit extraction.
byte & mask // will give you the most significant bit.
byte <<= 1; // will left sift the byte by 1 so that you can read the next MSB.

you can use the read bits to descend the huffman tree till you reach a leaf node, at which point you've decoded a symbol.

codaddict
When you shift the byte's bits over by 1 is the least significant bit(right most) = Null?
Azreal
@Azreal: It will be zero. If 0xFF (1111 1111) is left shifted we will get 0xFE (1111 1110)
codaddict
Don't you mean byte>>=1?
jmucchiello
A: 

Depending on what you're doing with the bits, it may be easier to read by the 32-bit word rather than by byte. In either case you're going to be doing mask and shift operations, the specifics of which are left as the proverbial exercise for the reader. :-) Don't be discouraged if it takes several tries; I have to do this sort of this thing moderately often and I still get it wrong the first time more often than not.

ceo
Reading by anything longer than a byte can cause endian issues. Don't do it.
jmucchiello