Sign in to follow this  
Gumgo

file.read( pointer, size ) function driving me crazy... won't read 26?

Recommended Posts

Okay I'm about to go insane... For some reason this function started not doing its job! Here's the problem: I have an uncompressed TGA loader set up that was working before. Basically, it reads the 18 bytes of header and figures out the size of data to read, then reads it all. Here's the code for reading the block of data:
file.read( (char*)Data, TGA.imageSize );
if (file.fail())
{
	delete[] Data;
	file.close();
	return;
}

Now here's the weird thing... I noticed it was failing to load certain images. At first I thought it was the size of the images... it always loaded images like 128x128, 64x64, etc. fine, but when I tried to load something larger like 256x256 or 512x512 it fail()ed. I tried this:
for (int ii = 0; ii < TGA.imageSize; ii += 1)
{
	file.read( (char*)Data, 1 );
	if (file.fail())
	{
		std::stringstream ss;
		std::string str;
		ss << ii;
		str = ss.str();
		MessageBox( NULL, str.c_str(), "", MB_OK );
		break;
	}
}

And got different values for ii with different images. It never failed for 64x64, 128x128, but for 256x256, one image I got a fail after 9 bytes read and another after 180362. On a 512x512 I got 6939. Also, after it would fail, file.tellg() would return -1. Then I decided to check what byte was at the point-of-failure. I found that on all the images, the byte it always failed at was one with a value of 26 (1A). However, it doesn't always seem to be the first occurrence of that byte in the file. I checked the 64x64 and 128x128 images - sure enough, there wasn't byte 26 anywhere in either file. So... am I just missing something very obvious or is something really really weird happening? I can't imagine why that function would fail to read a certain byte but that's what it seems to be doing! Any suggestions on what to do would be very helpful cause I'm completely stuck.

Share this post


Link to post
Share on other sites
Firstly, because ifstream.fail() returns whether the failbit or the badbit is set, I think you should also call ifstream.bad() to find out whether it's the failbit or the badbit that is set.

Share this post


Link to post
Share on other sites
Ooooooooookay... well I just wasted about 5 hours of my life... I forgot to open the file with the parameter std::ios::binary and so it was opening in text mode... turns out byte 26 marks the end of a line.

Problem solved I guess. [dead]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this