Alfredo Liardo
Alfredo Liardo

Reputation: 115

writing bits into a c++ file

I'm working on Huffman coding and I have built the chars frequency table with a

std::map<char,int> frequencyTable;

Then I have built the Huffman tree and then i have built the codes table in this way:

std::map<char,std::vector<bool> > codes;

Now I would read the input file, character by character ,and encode them through the codes table but i don't know how write bits into a binary output file. Any advice?

UPDATE: Now i'm trying with these functions:

void Encoder::makeFile()
{
char c,ch;
unsigned char ch2;
while(inFile.get(c))
{
    ch=c;
    //send the Huffman string to output file bit by bit
    for(unsigned int i=0;i < codes[ch].size();++i)
    {
        if(codes[ch].at(i)==false){
            ch2=0;
        }else{
            ch2=1;
        }
        encode(ch2, outFile);
    }
}
ch2=2; // send EOF
encode(ch2, outFile);

inFile.close();
outFile.close();
}

and this:

void Encoder::encode(unsigned char i, std::ofstream & outFile)
{
int bit_pos=0; //0 to 7 (left to right) on the byte block
unsigned char c; //byte block to write

if(i<2) //if not EOF
{
    if(i==1)
        c |= (i<<(7-bit_pos)); //add a 1 to the byte
    else //i==0
        c=c & static_cast<unsigned char>(255-(1<<(7-bit_pos))); //add a 0
    ++bit_pos;
    bit_pos%=8;
    if(bit_pos==0)
    {
        outFile.put(c);
        c='\0';
    }
}
else
{
    outFile.put(c);
}
}

but ,I don't know why ,it doesn't work, the loop is never executed and the encode function is never used, why?

Upvotes: 4

Views: 15936

Answers (2)

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36441

You can't write a single bit directly to a file. The I/O unit of reading/writing is a byte (8-bits). So you need to pack your bools into chunks of 8 bits and then write the bytes. See Writing files in bit form to a file in C or How to write single bits to a file in C for example.

Upvotes: 5

harper
harper

Reputation: 13690

The C++ Standard streams support an access of the smallest unit the underlying CPU supports. That's a byte.

There are implementation of a bit stream class in C++ like the Stanford Bitstream Class.

Another approach could use the std::bitset class.

Upvotes: 0

Related Questions