0

I am working on a compressor which compresses text files using Huffman coding in C++. After I perform the encoding, I get a bitstring(assume arbitrary length or say length = 2611) which represents the encoded file. Now, I want to write this bitstring to a binary file. How can I do this? Can bitset be of some use?

I am aware of the fact that the file size has to be integer number of bytes, so I will pad the bitstring to make the length a multiple of 8.

I saw some other related questions but they used pre-determined size bitset. In my case the bitstring can be quite long.

Thanks in advance!

PS: I would only want to write to a binary file, not a text file, otherwise it will make no sense to encode.

EDIT1: take for example the bitstring - "10010101010010101010101011101100001001000101011110110101001010101001001010110101" Length is 80 that wld mean 10 bytes. I want to write these 10bytes into a binary file. How can I do this beginning with the bitstring?

8
  • 1
    In terms of actual C++ code, what is a "bitstring"? How will it be represented? Commented Mar 9, 2021 at 14:27
  • @PaulMcKenzie by bitstring I meant I have a binary string. A string with all characters either 1 or 0. Commented Mar 9, 2021 at 14:28
  • @PaulMcKenzie I am storing it as a c++ string. Commented Mar 9, 2021 at 14:30
  • You have to convert it to bytes. Then write those to the file. Commented Mar 9, 2021 at 14:46
  • BTW: All files are binary on my computer. ;) Commented Mar 9, 2021 at 15:46

2 Answers 2

1

Funnily enough I'm doing the exact same thing.

After you check a character in your Huffman codes and take bitstring from it you would want to do something like

ofstream encoder(outputFilePath, ios::out | ios::binary | ios::app);

This ofstream has the proper flag to write binary strings.

after that you'd just have to write the output to a file.

Sign up to request clarification or add additional context in comments.

2 Comments

The OP appears to need help with writing the bits, not just opening a binary file.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Does this example respond to your question, in terms of writing bistsets into a file?

As far as i know, you can write bitset directly to a file

#include <iostream>
#include <string>
#include <bitset>
#include <fstream>

int main ()
{
  std::ofstream ofs ("testfile", std::ofstream::out);
  std::bitset<16> foo; // not intialized
  std::bitset<16> bar (0xfa2);
  std::bitset<16> baz (std::string("0101111001"));

  std::cout  << "foo: " << foo << '\n';
  std::cout << "bar: " << bar << '\n';
  std::cout  << "baz: " << baz << '\n';

  ofs << "foo: " << foo << '\n';
  ofs << "bar: " << bar << '\n';
  ofs << "baz: " << baz << '\n';
  ofs.close();
  return 0;
}

This code would output in the console:

foo: 0000000000000000
bar: 0000111110100010
baz: 0000000101111001

and the same output in the file testFile

4 Comments

Sadly, this won't work in my case. Correct me if I am wrong, if I use bitset of size 8(or any other fixed size) and initialize it with the bitstring(the way you did for baz) it will clip the bitstring to that many bits, therefore I will lose my encoded bits.
Thanks for your question and comment. Could you provide an example of input/ouput you would like to allow the community to help you please? You may edit your question and add an example
Added an example bitstring.
I don't seem to understand your example. What do you input to your program and what do you need as an ouput. I don't understand: "How can I do this beginning with the bitstring?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.