• C

Writing 5 bits at the time to a file


I have assignment for some wierd compression alghoritam that will read
in from a file convert characters to 5 bit codes and then write out
version of the original file. For example if input file contains string
and the codes are A=0, M=12,I=8,R=17 i am supposed to write out 3 byte file

The problem? How do I figure out shifting because I can only write out bytes
and not
bits...I figure out I would "create" a byte and write it but the codes will
In my example I would have something like this in binary

00000001    00010001    00010000

where first 5 bits are hex 0, second 5 bits are 12 and so on...
Sometimes one created byte is based on codes of 3 different input
I have a loop in which I read a char at the time from input file, create a
int code and now I have to figure out
an alghoritam that would create a byte ( a char) that I can write out to
output file

// get char at the time from input
while ((curr_chr = getc(in_file)) != EOF)
        // get code for it
        code = getCode(curr_chr);

       // shifting code ......

        // write created byte...
        fwrite(&wbyte, 1, 1, out_file);

Any help is much appreciated....

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you read in a char and write out a byte, you are unlikely to achieve any compression !!!
what you can do is, wait until you have sufficient chars
e.g. write out 16 chars at once .... this implies that you need an input buffer of 16*8=128 bytes and an output buffer of 16*5=80 bytes ...
problem is how to fill this buffer selectively .... this is going to be a bit tricky
1. initially set your output buffer to all 0s
2. maintain a mask of  80 bits... intially, you will have first 5 bits set to 1 and rest 5 bits set to 0
In a loop:::
3. read 1 char from input buffer and convert it into 5 bits ....
4. replicate these 5 bits 16 times in a temporary buffer...
5. && this temporary buffr with the mask ....
6. || this result with your output buffer
7. >>mask by 5 places ....
8. back to step 3 until you have exhausted input buffer

after this step, you would have 80 bytes of compressed data in your output buffer to write ....

A problem would arise if you end of your input is not eactly fitting your input buffer.. While uncompressing, how would you know that file ended .... To overcome this, you can either use headers (put overall length in header) or you can usse padding with some special character


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zambakAuthor Commented:

Between the time i posted a question your answer I tried following which is probably something you are suggesting also....The least multiple for 5 (number of bits of compression code) and 8 (number of bits in byte)  is 40.....So I can write globs of 40 bits (5 bytes) at the time for every 8 characters of input.

My function reads in 8 characters from input file into a char buffer. Then I have unsigned char glob buffer which is 5 bytes long. I take the original 8 chars and do bit shift manipulation. Then I can fwrite the glob to a file...In case i get EOF before full set of 8 i just padd the array.
And yes I am keeping a header of the input file's original size in bytes which will tell me where to "cut off" when decompressing.

Is this what you were reffering also?

Code of my function is below

void compressFile(const char *filename)

    struct stat fa;                 // file attributes structure
    FILE        *in_file;           // pointer to original file
    FILE        *out_file;          // pointer to a compressed file

    char        curr_chr;           // current date without null terminator;
    char        input[8];
    char        glob[5];
    int         i,j,done=0;

    // open original file for reading
    in_file = openFile(filename, "r");
    // obtain attributes for in file
    fstat(fileno(in_file), &fa);
    fprintf(stdout,"|>>> Compressing file \"%s\" (filesize = %ld bytes) on %s\n", filename, fa.st_size, getCurrentDate());
    // if file is empty return
    if (fa.st_size == 0)
        fprintf(stdout, "|>>> File is empty! Nothing to compress...\n");
    // open output file for writing
    out_file = openFile("output.cp", "w");

    // write the header with date and file size out
    fwrite(getCurrentDate(), 8, 1, out_file);
    fwrite(&(fa.st_size), 4, 1, out_file);

    while (!done)
        // clear out input bufffer
        // fetch 8 characters at the time used to create 5 byte glob
        for (i=0; i<8; i++)
            curr_chr = getc(in_file);
            if (curr_chr == EOF)
                for (j=i; j<8; j++)
                    input[j] = 'A';    // this will padd with 00000 */
                done = 1;
                input[i] = curr_chr;
        // create a glob based on input
        glob[0] = (getCode(input[0]) << 3) + (getCode(input[1]) >> 2);
        glob[1] = ((getCode(input[1]) & 0x03) << 6) + (getCode(input[2]) << 1) + (getCode(input[3]) >> 4);
        glob[2] = ((getCode(input[3]) & 0x7F) << 4) + (getCode(input[4]) >> 1);
        glob[3] = ((getCode(input[4]) & 0x01) << 7) + (getCode(input[5]) << 2) + (getCode(input[6]) >> 3);
        glob[4] = ((getCode(input[6]) & 0x07) << 5) +  getCode(input[7]);


        // write out the glob to an output file
        fwrite(&glob, 5, 1, out_file);

        //printf("Current character = [%c] ASCII=[0x%X]  code = 0x%X\n", curr_chr, (int)curr_chr, code);
    } // end while

    // close files

i was referring to exactly that... you are on the right track

Cheers :o)

You have to set bit by bit in wbyte.

Convert the code in to a binary array of 5 elements. (array is preferable)

Use a mask which is initially 0x80. Start from first element of array. If it is one then bitwise or the wbyte with the mask else do not or the mask. Now left shift the mask by 1 bit. Check if the mask is 0. If mask is 0 then write the created byte and reset mask to 0x80 and wbyte to 0 and continue.

It would be something like:

mask = 0x80
wbyte = 0;
while ((curr_chr = getc(in_file)) != EOF)
       // get code for it
       code = getCode(curr_chr);

       //convert to binary array bincode

              if (bincode[i] == 1)
            wbyte = wbyte | mask;
              mask = mask>>1;
              if (mask == 0)
             // write created byte...
             fwrite(&wbyte, 1, 1, out_file);
             mask = 0x80;
             wbyte = 0;


sorry i was late. I did not refresh the page for quite some time
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.