I have a file which I'm trying to write to. I opened it with an object of fstream class in binary mode and out mode (ios::binary | ios:ut).

Now, I have no problems reading/writing characters to the file. I also have no problems reading/writing a structure to the file.

Code:
struct record
{
   int i;
   char *name;
}student;

void write()
{
   fstream newFile;

   //yada yada bla bla... (Insert rest of codes here)   

   newFile.write(reinterpret_cast<const char *>(&student);
   newFile.write("Male");
}
This code is output in binary format, thus, when you open the text file with notepad, some of it is intelligible.

The problem is, now I want to write a binary file, inputting the binary streams in variable sized length of bits.

Eg. I want to output "1010 100 001111 011 01 1100" to the file, as binary code, not characters.

is there any way I can do this?

1) I've tried using bool datatype with true/false. but when I write the boolean variable into the file, it still outputs them in 8 bit chunks.
I checked in the debugger and boolean datatypes take up 1 whole byte although there are only 2 combinations available. "0x00" AND "0x01"

2) i tried enumerating my own boolean values, but there seems to be no binary format in C++. "0b". is there any other way that i don't know about, or did i do anything wrong?

3) is there anyway to do this with bitwise manipulation? 'cos all the bitwise manipulations i know doesn't add bits to a stream, but it just change their values.

4) is there an already known and available method for writing just 1 bit to a stream?


any help would be appreciated, thank you.