This code outputs a file with 101011 but it was not a binary file.
I thought about using write function to output the binary file, but it takes char pointer and the length. Is there any way to solve this problem?? By the way, the file size must be compressed. I tried to output binary file with string but the size was increased.. This is for Huffman encoding and decoding!!
By 'compressed' I assume you mean it's bitpacked so that there is no padding between bitsets in the file.
You will not be able to do this in a single function call. At least not without writing your own.
Files work with 8-bit bytes. You cannot write any value smaller than a single byte. So if you have a 10-bit value, that is going to occupy 16 bits (2 bytes) in the file. The extra 6 bits are "padding".
If you want to remove that padding, you'll have to run all i/o through some kind of class or function which takes bits out of the bitset, and writes them to the file only after it has accumulated 8 bits of data.
@DTSCode: I wouldn't worry too much about a byte being less/greater than 8-bits in length unless your software targets some specialised hardware. You can perform a check to validate the length of a byte, but 9 times out-of 10 the length of a byte will be 8-bits.
Even so, if you did target some specific piece of hardware, you'd read the hardware's corresponding specification and check the size of a byte. If the specification does not state the size of a byte, you have two options:
1) assume a byte on the targeted hardware has a size of 8-bits or...
2) contact the hardware's vendor and inquire about it.