I have created a bitset using std::bitset<8> bits
which is equivalent to 00000000
i.e., 1 byte.
I have output file defined as std::ofstream outfile("./compressed", std::ofstream::out | std::ofstream::binary)
but when I write the bits
using outfile << bits
, the content of outfile
becomes 00000000
but the size of file is 8 bytes. (each bit of bits
end up taking 1 byte in the file). Is there any way to truly write byte to a file? For example if I write 11010001
then this should be written as a byte and the file size should be 1 byte not 8 bytes. I am writing a code for Huffman encoder and I am not able to find a way to write the encoded bytes to the output compressed file.
Asked
Active
Viewed 744 times
-1

Abhinav
- 429
- 5
- 13
-
1The output operator `<<` is *textual*, it will write as text. You need to [write](https://en.cppreference.com/w/cpp/io/basic_ostream/write) the raw data instead. – Some programmer dude Feb 17 '20 at 18:25
1 Answers
1
The issue is operator<<
is the text encoding method, even if you've specified std::ofstream::binary
. You can use put
to write a single binary character or write
to output multiple characters. Note that you are responsible for the conversion of data to its char
representation.
std::bitset<8> bits = foo();
std::ofstream outfile("compressed", std::ofstream::out | std::ofstream::binary);
// In reality, your conversion code is probably more complicated than this
char repr = bits.to_ulong();
// Use scoped sentries to output with put/write
{
std::ofstream::sentry sentry(outfile);
if (sentry)
{
outfile.put(repr); // <- Option 1
outfile.write(&repr, sizeof repr); // <- Option 2
}
}

Travis Gockel
- 26,877
- 14
- 89
- 116
-
Thank you for the answer. I have just one more question and then I will accept your answer. In the line `char repr = bits.to_ulong();` you have written the comment `your conversion code is probably more complicated than this`. What do you mean by this? – Abhinav Feb 18 '20 at 14:10
-
Casting the `unsigned long` returned from `to_ulong` into a `char` will only work when the bitset uses 8 or fewer bits. If you use more than that, things will get more complicated. – Travis Gockel Feb 18 '20 at 15:02
-