Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:54:38+00:00 2026-06-06T18:54:38+00:00

I am using huffman algorithm to develop a file compressor and right now I

  • 0

I am using huffman algorithm to develop a file compressor and right now I am facing a problem which is:

By using the algorithm to the word:
stackoverflow, i get the following result:

a,c,e,f,k,l,r,s,t,v,w = 1 time repeated
o = 2 times repeated

a,c,e,f,k,l,r,s,t,v,w = 7.69231%
and
o = 15.3846%

So I start inserting then into a Binary Tree, which will get me the results:

o=00
a=010
e=0110
c=0111
t=1000
s=1001
w=1010
v=1011
k=1100
f=1101
r=1110
l=1111

which means the path for the character in the tree, considering 0 to be left and 1 to right.

then the word “stackoverflow” will be:
100110000100111010011111000010110110111011011111001010

and well, I want to put that whole value into a binary file to be in bits, which will result in 47bits, which would happen to be 6bytes, but instead I can only make it 47bytes because the minimun to put into a file with fwrite or fprintf is 1byte, by using sizeof(something).

Than my question is: how can I print in my file only a single bit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:54:40+00:00Added an answer on June 6, 2026 at 6:54 pm

    Just write the “header” to the file: the number of bits and then “pack” the bits into bytes padding the last one. Here’s a sample.

    #include <stdio.h>
    
    FILE* f;
    
    /* how many bits in current byte */
    int bit_counter;
    /* current byte */
    unsigned char cur_byte;
    
    /* write 1 or 0 bit */
    void write_bit(unsigned char bit)
    {
        if(++bit_counter == 8)
        {
            fwrite(&cur_byte,1,1,f);
            bit_counter = 0;
            cur_byte = 0;
        }
    
        cur_byte <<= 1;
        cur_byte |= bit;
    }
    
    int main()
    {
        f = fopen("test.bits", "w");
    
        cur_byte = 0;
        bit_counter = 0;
    
        /* write the number of bits here to decode the bitstream later (47 in your case) */
        /* int num = 47; */           
        /* fwrite(num, 1, 4, f); */
    
        write_bit(1);
        write_bit(0);
        write_bit(0);
        /* etc...  - do this in a loop for each encoded character */
        /* 100110000100111010011111000010110110111011011111001010 */
    
        if(bit_counter > 0)
        {
             // pad the last byte with zeroes
             cur_byte <<= 8 - bit_counter;
             fwrite(&cur_byte, 1, 1, f);
        }
    
        fclose(f);
    
        return 0;
    }
    

    To do the full Huffman encoder you’ll have to write the bit codes at the beginning, of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here when I get file size using stat() it gives different output, why does
I'm writing an encoding/decoding .COM program using Huffman algorithm for dos 8086 (16-bit tasm
I have implementated a simple compressor using pure huffman code under Windows.But I do
I coded a Huffman Algorithm. I read a file into a String and then
I am now using Visual C++ 2010 to open a txt file. fp =
What algorithm does java.util.zip is using in compression? LZW, huffman, run-length? Thanks
Using the Redis info command, I am able to get all the stats of
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
i am trying to create a Huffman tree by reading in a file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.