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

  • SEARCH
  • Home
  • 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 5961141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:55:25+00:00 2026-05-22T18:55:25+00:00

I am working on some homework for Huffman coding. I already have the Huffman

  • 0

I am working on some homework for Huffman coding. I already have the Huffman algorithm completed, but need to slightly alter it to work with binary files. I have some spent some time reading related problems, and perhaps due to my lack of understanding of data types and binary files, I am still struggling a bit, so hopefully I am not repeating a prior question (I won’t be posting code related to the huffman part of the program).

Here is the key phrase: “You can assume that each symbol, which will be mapped to a codeword, is a 4-byte binary string.”, and what I think I know is that Char represents one byte and unsigned int represents four byte, so I am guessing I should be reading the input four bytes at a time into a unsigned int Buffer and then collect my data for the Huffman part of the program.

int main() {
    unsigned int buffer;
    fstream input;
    input.open("test.txt", ios::in | ios::binary);


    while(input) {
        input.read(reinterpret_cast<char *>(&buffer), 4);
        //if buffer does not exist as unique symbol in collection of data add it
        //if buffer exists update statistics of symbol
    }
    input.close();
}

Does this look like a good way to handle the data? How should I handle the very end of the file if there are only 1,2, or 3 bytes left? So then I am just storing buffer as unsigned int in a struct. Just out of curiosity how would I recast buffer to a string of characters?
Edit: What’s the best way to store the header of a Huffman compressed a file?

  • 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-05-22T18:55:26+00:00Added an answer on May 22, 2026 at 6:55 pm

    Does this look like a good way to handle the data?

    Instead of casting a pointer, I would suggest using union of int and char [4] and passing pointer to the char array as you should be. Don’t know what’s the rest of the logic, so can’t say if the actual handling (which is not in the code you posted) is done in a good way, but it seems to me rather trivial.

    How should I handle the very end of the file if there are only 1,2, or 3 bytes left?

    Assuming each symbol is 4 bytes long, I would expect that not be a valid input.

    So then I am just storing buffer as unsigned int in a struct. Just out of curiosity how would I recast buffer to a string of characters?

    Why would you do that? In your data, a “character” is 4 bytes. But you can just use casting to array of bytes if you want (or, better, use bitwise operations to extract the actual bytes, if the order matters).

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

Sidebar

Related Questions

I'm working on some homework for my compiler class and I have the following
So I am working on some homework and I have to complete a size
I'm working through some homework and a question on a previous exam paper asks
I was working on my advanced calculus homework today and we're doing some iteration
I have been working on some legacy C++ code that uses variable length structures
I am working on some homework. I cannot get the footer to fit properly
I've inherited some code that I need to debug. It isn't working at present.
It might be for pedantic purposes but not homework. I have below question about
Having some issues with one small function I'm working on for a homework assignment.
I am working on some homework for class, and decided to add some extra

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.