Funky std::ifstream behavior [C++]

Started by
1 comment, last by hughiecoles 12 years, 8 months ago
I've been writing a file loader today, and the get pointer keeps jumping.

The file I'm loading is binary (.3ds), and for some reason after my first read, the get pointer jumps from 0 to 3586.
The code is:

std::ifstream file(filepath,std::ios_base::in||std::ios_base::binary);
if(file.is_open() == false)
return false;


//read header chunk
ChunkHeader magicNumber;
file.read((char*)&magicNumber.chunkID,2);
std::streampos pos = file.tellg(); //at this point, after reading only 2 bytes, pos == 3586
file.read((char*)&magicNumber.chunkEnd,4);
pos = file.tellg();


if(magicNumber.chunkID != CHUNKTYPE_MAIN || !file)
{
file.close();
return false;
}

pos = file.tellg();


I even commented out all other code except the load function, and the behavior still persists.
Any suggestions?



*EDIT - And after the first read(), gcount() returns 2
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Advertisement
Hello! Maybe your problem is in the way you open your file. You use an ifstream so you don't have to specify ios::in, because it opens it for reading anyway. And instead of || you would have to use the single |.

Try it like this:


std::ifstream file(filepath, ios::binary);


I don't really know if this will help you but you can give it a try.

Hello! Maybe your problem is in the way you open your file. You use an ifstream so you don't have to specify ios::in, because it opens it for reading anyway. And instead of || you would have to use the single |.

Try it like this:


std::ifstream file(filepath, ios::binary);


I don't really know if this will help you but you can give it a try.



Thank you so much. It's amazing what a fresh set of eyes can do (+1 for pairs programming I guess). I didn't notice the logical OR typo and the compiler let it slide.
Everything's working now.

Thanks again, I had spent a few frustrating hours staring at this and overlooked it.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Running this might be illuminating:

#include <iostream>

int main()
{
std::cout << "std::ios_base::app: " << std::ios_base::app << '\n';
std::cout << "std::ios_base::in: " << std::ios_base::in << '\n';
std::cout << "std::ios_base::binary: " << std::ios_base::binary << '\n';
std::cout << "std::ios_base::in | binary: " << (std::ios_base::in | std::ios_base::binary) << '\n';
std::cout << "std::ios_base::in || binary: " << (std::ios_base::in || std::ios_base::binary) << '\n';
}

This topic is closed to new replies.

Advertisement