I'm trying to go more to the C++ side of things and break away from plain old C and part of that is using fstream for file access instead of fread/fwrite. I know how to open a file for reading and/or writing, but when it comes to the actual reading and writing of binary files I am still not sure that I am coding it properly. Below is a chunk of code designed to read in a section of binary data from a GTA:SA save game. The code compiles without warning or error, but I just feel uneasy about it since everything has to be type-cast to char* just to read in. Can somebody offer me some advice on reading and writing data with fstream? I mean binary files, not text-mode.
//Read the car object data from the current stream location
pFile->read((char*)this->pPosition, 12); //float[3]
pFile->seekg(ios::cur, 4);
pFile->read((char*)&(this->ucFlags), 1); //unsigned char
pFile->seekg(ios::cur, 1);
pFile->read((char*)&(this->sModelID), 2); //short
pFile->read((char*)this->pMods, 30); //short[15]
pFile->read((char*)this->pColors[0], 1); //unsigned char[4]
pFile->read((char*)this->pColors[1], 1); //unsigned char[4]
pFile->read((char*)this->pColors[2], 1); //unsigned char[4]
pFile->read((char*)this->pColors[3], 1); //unsigned char[4]
pFile->read((char*)&(this->ucRadioStation), 1); //unsigned char
pFile->read((char*)this->pVariation, 2); unsigned char[2]
pFile->read((char*)&(this->ucBombType), 1); //unsigned char
pFile->read((char*)&(this->ucPaintJob), 1); //unsigned char
pFile->read((char*)&(this->ucNitrous), 1); //unsigned char
pFile->read((char*)this->pRotation, 3); //unsigned char[3]
pFile->read((char*)&(this->ucAlign), 1); //unsigned char
I am specifically not sure whether or not I coded the color section properly since I am specifying a specific point in the color array. Thanks for any help you can offer.
-
Sephiroth