I've read the LZW compression algorithm. And i know i must use 12 bit number to code the string.
But i dont know how to write this code 12 bit to file. Because when using fwrite function, it just accepts bytes (not bit).
How can i do that with bit. Someone tell me do this :
write 16bit (2 bytes) , certainly remaining 4 bit from the next code. And add the next 16 bit to file, surely contains 8 bit from the next code, and so one. I think it is right, isn it?
But how i can do that?
Please suggesting me all the technologies i have to learn for this purpose.
Or give me the link to page that contain this topic or tutorial.
Thanks

2. i dont knwo anything about LZW algorithm
or even fi what your saying is correct,
but all you need to do to use binary
i believe is open the file in binary,

Code:
```ifstream file;
file.open("myfile.txt", ios::in | ios::binary);```

3. Code:
```#include <iostream>
#include <bitset>

using namespace std;

int main()
{
// 16 bits each
short num;

// file to be written
ofstream file_in("file");

// 15 is equal to 000...01111
num = 15;
file_in << num;
file_in.close();

// reseting, not necessary, but to make sure it works
num = 0;

// Lets just read 15 again
ifstream file_out("file");
file_out >> num;

cout << "The number is " << num << endl;
cin.get();

return 0;
}```
Hope that helps... also, have a look at bitset class, it might be useful.

4. but all you need to do to use binary
i believe is open the file in binary,
The issue is how to write bits when all you can work with are bytes.

john_tran:
You must arrange the bits 8 at a time, into a byte. If you want to write 10010, then 00010, you have to somehow make it into 10010000 and 10000000 instead (I put extra 0's after the 10 so that it would be 8 bits in total). Then you can write them one at a time to the file.