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

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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).

##### Share on other sites
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.

##### Share on other sites
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.

• 21
• 13
• 9
• 17
• 13