reading binary

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";
		return false;
	}

	inFile.read(reinterpret_cast<char *>(val), size);
	return true;
}
// Calling Function
if (fileMode == (std::ios::in | std::ios::binary)){
	void *ptr = &data;
	if (!readBin(ptr, sizeof(data))){
		return false;
	}
}


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));
}
Last edited on
Topic archived. No new replies allowed.