Search code examples
c++binarybit-manipulationfwrite

Binary Output c++


I'm working on a C++ text file compression program for a class. I have everything working except being able to output to a file in binary mode. I am using:

FILE* pFile;
pFile = fopen(c, "wb");

In order to open the file for writing in binary mode,

bool Buffer[8] = { 0, 0, 0,0, 0,0, 0,0 };

Im using a buffer of bools (initialized to all 0's) to store the 1's and 0's to store each byte of data until I use fwrite.

vector<bool> temp2 = bitstring[temp];//bitstring refers to a vector in a map at key [temp]
for (vector<bool>::iterator v = temp2.begin(); v != temp2.end(); ++v)
        {

            if (j < 8)//To create an 8 bit/byte buffer
            {
                if (*v == 1)//Checks the vector at that position to know what the bit is.
                    Buffer[j] =1;//sets the array at 'j' to 1
                else
                    Buffer[j] =0 ;//sets the array at 'j' to 0

                j++;
            }
            else //once the Buffer hits 8 it will print the buffer to the file
            {

                fwrite(Buffer,1,sizeof(Buffer), pFile);

                clearBuffer(Buffer);//Clears the buffer to restart the process.
                j = 0;

            }

        }

the (vector iterator )is going through a vector of bools that is assigned to a specific character, essentially a unique binary string that represents the character. My problem is that instead of outputting as binary in the buffer, its outputting essentially ASCII characters in binary mode, instead of just the digits as binary. Which ends up making the file WAY bigger than it needs to be. How could i change the buffer to just output bits. I was told to use bitwise operators, but I can't find very much documentation on implementing this in c++. Any help is appreciated, and


Solution

  • I would use std::bitset in the first place, definitely flexible for this purpose

    std::bitset<8> BufferBits;
    vector<bool> temp2 = bitstring[temp];//bitstring refers to a vector in a map at key [temp]
    for (vector<bool>::iterator v = temp2.begin(); v != temp2.end(); ++v)
            {
    
                if (j < 8)//To create an 8 bit/byte buffer
                {
                    if (*v == 1)//Checks the vector at that position to know what the bit is.
                        BufferBits[j] =1;//sets the array at 'j' to 1
                    else
                        BufferBits[j] =0 ;//sets the array at 'j' to 0
    
                    j++;
                }
                else //once the Buffer hits 8 it will print the buffer to the file
                {
                    unsigned long i = BufferBits.to_ulong(); 
                    unsigned char c = static_cast<unsigned char>( i );
                    fwrite(&c, sizeof(char), 1, pFile);
    
                    BufferBits.reset();//Clears the buffer to restart the process.
                    j = 0;
    
                }
    
            }
    

    Notice: I just considered the issues regarding your bit-vector