I want to write individual bits of information to a file. Is there any way to do that in C? All the results I've looked up so far have just shown me how to print the characters '1' and '0' to a file, which is not what I want to do. To clarify, if I were to write '10100101' to a file, it should only take up one byte.
-
2Pack the bits into bytes and write to a file in binary mode. – lurker Oct 10 '19 at 02:25
-
Possible duplicate of [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – klutt Oct 10 '19 at 02:28
-
1`'10100101'` is a string of the characters `'1'` and `'0'`to visually display individual bits in a byte. In order to write it as one byte, you'll need to convert it to an actual number, not a string, and write it in binary (not text) mode. That should give you enough information to do some searching and make an effort to solve the problem yourself. – Ken White Oct 10 '19 at 02:33
2 Answers
The following code will write a byte with the binary value 10100101 (which is A5 in hexadecimal) to a file. It will be written in binary mode, i.e. not as a text string.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( void )
{
FILE *fp;
unsigned char to_write;
size_t ret_val;
//attempt to open file
fp = fopen( "testfile.bin", "wb" );
if ( fp == NULL )
{
fprintf( stderr, "Error opening file!\n" );
exit( EXIT_FAILURE );
}
//set value to write
to_write = 0xA5;
//attempt to write to file
if ( fwrite( &to_write, sizeof to_write, 1, fp ) != 1 )
{
fprintf( stderr, "Error writing to file!\n" );
exit( EXIT_FAILURE );
}
//cleanup
fclose( fp );
return EXIT_SUCCESS;
}

- 22,760
- 4
- 24
- 39
You can implement your own "bit stream" abstraction, like this:
#include <stdio.h>
void putbit(int);
void flushbits();
int main()
{
putbit(1);
putbit(0);
putbit(1);
putbit(0);
putbit(0);
putbit(1);
putbit(0);
putbit(1);
flushbits();
}
static unsigned char bitbuf = 0;
static int nbits = 0;
void putbit(int b)
{
bitbuf |= b << (7 - nbits);
nbits++;
if(nbits == 8) flushbits();
}
void
flushbits()
{
if(nbits > 0) {
putchar(bitbuf);
bitbuf = 0;
nbits = 0;
}
}
This example writes to the standard output. In more realistic code, you'd be writing to some FILE *
stream you'd opened using fopen(…, "wb")
. (In that case, and if you're going for full generality, there's a mildly tricky problem to solve in associating the static bitbuf
and nbits
variables with the particular stream you have open, and especially if you want to support the possibility of having multiple bit streams open.)
In any code like this you have to resolve the bit order question: are the bits in your stream packed into bytes in left-to-right or right-to-left order? Here I've decided on left-to-right (meaning that a single call to putbit(1)
would result in the byte 0x80
getting written).
Another improvement of possible interest would be to replace the magic numbers 7
and 8
with CHAR_BIT-1
and CHAR_BIT
, respectively, which would make the code portable to machines with other than 8-bit bytes (if you can find one of those!).

- 45,437
- 7
- 70
- 103