ifstream not working right in a particular function without ofstream??

Started by
2 comments, last by mr BiCEPS 19 years, 6 months ago
I wonder why ifstream isn't working right in a particular function without opening an ofstream?? Here's an excerpt from a loadgame function.

ifstream in(filename, ios::in | ios::binary);
//...............................................
//...............................................

for(int i = 0; i < 16; ++i)
{
        in >> dateref->small_cupclans[0];
	in >> dateref->small_cupclans[1];
}
in.close();
//If you uncomment the below two lines, the loadgame function works properly, but not otherwise?!
//ofstream out("BLAA.txt", ios::out | ios::binary);	
//out.close();


Any ideas what might be causing this? I tried rebuild all too. I don't have any other ifstreams/fstreams/ofstreams opened.
Advertisement
A priori, it should work, can you describe how the code misbehaves? Have you checked the state of the stream after each operation (bad, fail and eof bits)?

Just checking, but you're using <fstream> and not <fstream.h>, right?

And by the way, the ios::in and ios::out are already implied respectively by ifstream and ofstream classes (they're only relevant when you create an fstream).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
yes, I'm using <fstream>. Well it simply doesn't load all the variables, only like the first 1% of them without the ofstream.. I also tried to put the ifstream code in a separated block( { }), which proved useless. But do you have any idea why the ofstream call makes it work? It makes no sense.
It does look strange. What compiler are you using? There are bugs in some versions of the C++ standard library. Still, I don't really think this is a bug with the standard library (but I could be wrong), I think it probably is stack corruption taking place somewhere in parts of code that you didn't post. When you have the extra ofstream in the code, there is space allocated on the stack for that as well, and I think whatever corrupts your data if it isn't there, corrupts the ofstream "struct" instead. That's just a guess though, can't really say without the rest of the code. Check the rest of the code to see if there are any runs outside array bounds or something similar.

This topic is closed to new replies.

Advertisement