In C if I write a single char to disk will it just be the single char or will it

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
have a terminating /0 at the end of it? Also how can a one byte char represent 256 values when it cannot be represented in hex by one digit?
 
Last edited:

degibson

Golden Member
Mar 21, 2008
1,389
0
0
No trailing null. One byte is one byte. Unless you're using a library that adds one; I recommend you don't do that.

1 bytes = 8 bits = 2 hex digits.
1 hex digit = 4 bits.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
When you write a byte as a byte (instead of first converting it to a string) it only uses 1 "character" of a file, just like it occupies 1 byte of RAM.

For an integer 32, it writes 4 bytes even though it could be a 10-digit decimal number when converted to a string.

In both cases, you must read it back the same way that you wrote it.

// size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

int32 foo ; // replace int32 with whatever your compiler uses for 32-bit integer

foo = 1234567890 ;
fwrite ( &foo , 4, 1, my_stream ) ; // should really be sizeof( int32 ) not 4

nb = fread ( &foo, 4, 1, my_stream ) ;