I need to read and write binary data in my program. After doing a little research, it seemed like an unsigned char array might be a good choice to store the data.
I open the file in binary mode using the ios::binary flag, but when I go to read or write, the ifstream::read() and ofstream::write() functions are expecting a char* and const char*.
So, I have to cast my unsigned char* to a char* every time I want to read or write.
I don't think this will make any difference but I'm starting to wonder if I should just use a regular char array to store the data instead. I have seen people use both char and unsigned char arrays for this purpose and I don't fully understand the difference.
Let's say I have 2 arrays:
char a[20];
unsigned char b[20];
Now I open a file in binary mode and read:
file.read(a, 20);
file.read((char*)b, 20);
Now I want to write this data to a new file so I open in binary mode again:
newfile.write(a, 20);
newfile.write((char*)b, 20);
What's the difference? Am I better off just using a char array instead of an unsigned char array for binary data?