I'm trying to condense some code, but am running into issues:
My original code was:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool InFileH::readBin(void* val, std::streamsize size){
if (inFile.tellg() + size > fileSize){
error += "EOF too soon";
returnfalse;
}
inFile.read(reinterpret_cast<char *>(val), size);
returntrue;
}
// Calling Function
if (fileMode == (std::ios::in | std::ios::binary)){
void *ptr = &data;
if (!readBin(ptr, sizeof(data))){
returnfalse;
}
}
This worked just fine. I'm working with exceptions now, and think I can condense this stuff:
1 2 3 4
if (inFileMode == (ios::in | ios::binary)){
if (inFile.tellg() + sizeof(data) > inFileSize) throw ExceptionReport(error += "EOF too soon");
inFile.read(reinterpret_cast<char *>(data), sizeof(data));
}
The issue is inFile.tellg() + sizeof(data), the code compiles if I don't use sizeof(data) or inFile.tellg(). Though seems to be causing a bigger issue as well.
I'm confused, all "size" was in the ReadBinary function was the sizeof(data).
Any ideas? Thanks
Edit: Looks to be solved, but still confused as it was working without the cast before hand.
1 2 3 4
if (inFileMode == (ios::in | ios::binary)){
if (inFile.tellg() + streampos(sizeof(data)) > inFileSize) throw ExceptionReport(error += "EOF too soon");
inFile.read(reinterpret_cast<char *>(&data), sizeof(data));
}