I need to compress an image(by block truncation),but I don't know
how to "write one bit" into the binary file, I have checked the function of C, the smallest unit are "byte" but not bit, if there are no way to "write one bit" into the binary file directly, then I would have to do some management before I write data into the file.
You can't write a bit to a file directly. You have to "collect" bits until you have at least one byte.
It's recommended to create a stream class that handles the bit shifting and packing.
int putbit(FILE *f,char buf[2],bool bit){//writes the bit to the file, when the buffer is full.
if(buf[0]==0){
fputc(buf[1],f);
buf[0]=7;
buf[1]=0;
}
buf[1]+=(int)bit<<buf[0]--;
}