programming
October 15, 2008 2:39 PM   Subscribe

What data type can I use to write one byte of data to a file in C?

On my system short int is 2 bytes. I know writing a character to a file will give me one byte, but I need some of them to have a msb of 1, not 0--which all ascii values do.
posted by amsterdam63 to Computers & Internet (11 answers total)
 
unsigned char.
posted by zsazsa at 2:44 PM on October 15, 2008


unsigned char
posted by veedubya at 2:45 PM on October 15, 2008


As others have said, unsigned char. Or, a lot of systems now have <machine/types.h> which will give you fun things like u_int8_t, u_int16_t, etc.
posted by sbutler at 2:51 PM on October 15, 2008


Your very vague "data" should usually be an array of "char" or "void", if it's opaque and meaningless to that level. Use write(fd, opaque_blob_pointer, N) to write the first N bytes to file descriptor fd.

Note, you have no guarantee that you will get N bytes. Check the return value of write to see how many it really wrote.
posted by cmiller at 2:56 PM on October 15, 2008


'char' can almost certainly store 256 distinct values, but half of them are negative on platforms where 'char' is the same as 'signed char'.

If using fopen, remember to give the "b" argument to open in binary mode. doesn't matter on unix, may or may not matter on today's macs, matters a great deal on godforsaken windows.

seconding that you should check all return values -- C and C++ don't do you any favors in terms of forcing you to handle errors, in contrast to environments like Python where all I/O errors are exceptions.

sbutler, In C99 the standard header defines integer types by width. uint8_t is almost certainly going to be the same as unsigned char on any system in wide use today.
posted by jepler at 3:45 PM on October 15, 2008


Can you give more details about what you're doing in total? Maybe we can suggest a more complete solution.
posted by demiurge at 4:27 PM on October 15, 2008


argh -- the standard header I intended to mention above is <stdint.h>
posted by jepler at 5:48 PM on October 15, 2008


If I read your question as just being about writing data out, then you can use ANY data type you want. fwrite doesn't know anything about the type of object you're writing out other than what you tell it about the object's size. So I can tell fwrite to write the first byte of a huge complicated structure and it will happily do so. write doesn't even care about the size of an object. It just wants a pointer to a location in memory and the number of bytes it you want it to write.

I presume that you want to do something with that byte other than write it out. That's why you should choose an appropriate type. unsigned char is a good first approximation. uint8_t from types.h is a better choice. I would use typedef to create a type. Chances are uint8_t is already typedef'ed from unsigned char but I like making my intent as explicit as possible.

Although the chances of you running into this are vanishingly small, there are machines where a byte is not eight bits wide. What's more not all machines using ASCII encoding. Char, signed or unsigned, is a data type that we can use to conveniently manipulate character data. ASCII is a scheme for representing character data. In most situations we can reason about chars and character data as if they were the same thing but there is a difference and we can't always ignore it.
posted by rdr at 6:25 PM on October 15, 2008 [1 favorite]


While the old standard of char or unsigned char works, many would agree that current style dictates that you explicitly state the size of your type by using one of the uint8_t-style types.

You could also do bit-field structs and precisely define the bit-length.
posted by Netzapper at 8:20 PM on October 15, 2008


Actually, I'm an ass... bitfield structs are non-portable.
posted by Netzapper at 8:21 PM on October 15, 2008


stdint.h is from C99. It contains a definition for uint8_t, which would be 1-byte. As others have said though, you can use a char or unsigned char. You just need a chunk of memory you want to store stuff in and stream out to a file.
posted by chunking express at 7:51 AM on October 16, 2008


« Older Looking for podcasts about music   |   I need some support here! Newer »
This thread is closed to new comments.