I have opened an ofstream file to write to, and I want to write 4 bytes of an unsigned int. I want the int to be zero, so I tried doing this:
outFile.write((char*)&unsigned int(0), sizeof(unsigned int)); // Expression must be an lvalue
unsigned int a = 0;
outFile.write((char*)&a, sizeof(unsigned int)); // This works fine
My understanding is that the lifetime of something like that unsigned int is up to the end of that line, in other words destroyed by the next semicolon. Is the only way to write something like this by creating a local beforehand? Also, I noticed that fstreams take char pointers, I was wondering is there any need for caution in copying stuff, for example in binary mode I expect it to write bytes exactly as it is in memory, but for example if I have an unsigned int of 255 will it copy this unsigned int bit for bit as it is? Shouldn't this have been something like a void pointer when dealing with raw memory copying?