OK, I haven't used streams in a while, and have just written this operator Overload below to Load a specific block of data from a file into my object.
[source]
std::ifstream& operator>>(std::ifstream &fs, Race &T)
{
char buffer[256];
int intbuffer[6];
std::string ip;
long liUID;
int size=0;
//Read in the Size of the 1st String
fs.read((char*)&size,sizeof(int));
//Read in the String
fs.read((char*)&buffer[0],size);
ip = buffer;
T.SetName(&ip);
//Description
fs.read((char*)&size,sizeof(int));
//Read in the String
fs.read((char*)&buffer[0],size);
ip = buffer;
T.SetDescription(&ip);
//Know The Above works due to Previous Debug code
//Next Line is causing an error
//this Line reads in 3 char, not 4 (which is size of long)
fs.get((char*)&liUID,sizeof(long));
T.SetUID(liUID);
printf("Number of Bytes Last Read in %i , should be %li\n",fs.gcount(),sizeof(long));
printf("%li\n",liUID);
return fs;
}
[/source]
OK, Each time the above function is called it reads in the size of the 2 strings (and the strings correctly).
Then it Moves on to read in the Long (Which incidentally is 999)
Instead of reading in 4 bytes, it reads in 3, which means that the next time the code is called, the file pointer is at the wrong point in the file, and so it reads in too many characters (size of next string has been left shifted a byte) and crashes my app.
I have been staring at this for hours now, and I cannot see the solution, although it is probably one of those annoiying little problems.
TIA,
K
[source]
std::ifstream& operator>>(std::ifstream &fs, Race &T)
{
char buffer[256];
int intbuffer[6];
std::string ip;
long liUID;
int size=0;
//Read in the Size of the 1st String
fs.read((char*)&size,sizeof(int));
//Read in the String
fs.read((char*)&buffer[0],size);
ip = buffer;
T.SetName(&ip);
//Description
fs.read((char*)&size,sizeof(int));
//Read in the String
fs.read((char*)&buffer[0],size);
ip = buffer;
T.SetDescription(&ip);
//Know The Above works due to Previous Debug code
//Next Line is causing an error
//this Line reads in 3 char, not 4 (which is size of long)
fs.get((char*)&liUID,sizeof(long));
T.SetUID(liUID);
printf("Number of Bytes Last Read in %i , should be %li\n",fs.gcount(),sizeof(long));
printf("%li\n",liUID);
return fs;
}
[/source]
OK, Each time the above function is called it reads in the size of the 2 strings (and the strings correctly).
Then it Moves on to read in the Long (Which incidentally is 999)
Instead of reading in 4 bytes, it reads in 3, which means that the next time the code is called, the file pointer is at the wrong point in the file, and so it reads in too many characters (size of next string has been left shifted a byte) and crashes my app.
I have been staring at this for hours now, and I cannot see the solution, although it is probably one of those annoiying little problems.
TIA,
K