# 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)
{
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 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 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.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978519

• 10
• 10
• 12
• 22
• 13