tags:

views:

228

answers:

4

Out of curiosity, why are we converting something to a char* when we write it to a ( binary ) file?

+14  A: 

Because the typical I/O functions that write, take a pointer to char. This is because someone considered that the most representative way of talking about the data stored in a binary file; it's just a bunch of the machine's smallest adressable word, in sequence. The C type name for that is char.

unwind
+7  A: 

char* merely represents a pointer to the beginning of a sequence of bytes, which is exactly what one expects a binary file to contain.

vezult
+2  A: 

Unwind and vezult have already answered your question, and I assume you know what a pointer is. But just in case you think of *converting something to a char** as an operation that actually somehow changes your data in memory (and, for example, may take more time if there's a lot of data) then note that such is not the behavior of getting a pointer.

Arjan
+2  A: 

Are you talking about fread() and fwrite()? The data they read or write are passed as void* (or const void*), so you don't have to convert.

But in C++ when you use, say, istream::read(), then the pointer to the reception buffer must be passed as a char*, so there is no implicit conversion.

Bastien Léonard