Filestream read() issues

closed account (Ly59GNh0)
I'm trying to read a 3D mesh file, but there seems to be something wrong with my file wrapper class, which is understandable since I never tested reading a binary file before... -_-

1
2
3
4
5
6
7
8
9
10
11
12
13
bool Utilities::File::ReadData(void* someData,int aSizeToRead)
{
	if (myStateFlags[READ] && myStateFlags[BINARY])
	{
		char* someDataAsCharPointer = static_cast<char*>(someData);
		myFileStream.read(someDataAsCharPointer, aSizeToRead);
		if (myFileStream.good() == true)
		{
			return true;
		}
	}
	return false;
}


After the line with the read() function, good() returns false. If I call the good() function before read() it returns true, so I'm sure that's where things go wrong.
aSizeToRead has the correct value, and even if I set it to 1 the same thing happens.
Am I using the read() function incorrectly?
Is void* someData dynamically allocated?
closed account (Ly59GNh0)
Yes, the call for the function looks like:

1
2
3
myRawFileData=new unsigned char [fileSize] ();

tempFile.ReadData(myRawFileData,fileSize);
Did you check if the stream is open? tempFile.is_open()
closed account (Ly59GNh0)
Just to clarify, tempFile isn't a filestream, it's an object of the file wrapper class I'm using.
I did however check in the ReadData function that the filestream it wraps is open before reading from it, and it is.
Then I think we really need to see the wrapper class.
closed account (Ly59GNh0)
Really? That's a lot of code to dump here for something that feels so isolated in that one function.
I might do that tomorrow if my teacher can't give me a simple solution.
Also, why don't you simply return MyFileStream.good() instead of using an if statement?
closed account (Ly59GNh0)
Because back when we created these utility classes I thought the code was easier to follow like that. I don't anymore.

Anyway, I managed to locate the issue. It wasn't as isolated as I thought.
My GetFileSize function did a seekg (0, std::ios::end)
Topic archived. No new replies allowed.