# ifstream opening file causes crash

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

## Recommended Posts

Hello, after getting my program working right the way I want it, I discovered some bugs. When I start my program the user can select a level and some of the data is read from a text-file. At the first attempt to load it everything works fine. Same with the second and third, but at the fourth it always crashed when I try to open a new file (ifstream in(file_name, ios::binary)). The filestream is properly closed after reading it (with in.close()), so I can not even guess where I have my error... ...perhaps you can give me a hint? Thanks in advance, Marco

##### Share on other sites
std::ifstream::clear()

##### Share on other sites
Thanks, but it did not work for me... the strange thing with that error is, that the open-function somehow crashes my program. I tried the clear-function before calling it and I tried to check the good-function, but everything seems fine. Until I try to open the file :(

Any other suggestions?

##### Share on other sites
Which compiler/standard library implementation are you using? IIRC, older service packs of MSVC 6 could cause this kind of problem.

##### Share on other sites
I am using Dev-C++ with the newest g++ compiler...

But in fact I do not think that the problem is to be found quite at the filestream stuff, but rather somewhere else (I know, not very specific).
The ifstream object is only local (in a function), so it gets destroyed after the function finished loading. And the strane thing is, that it works 3 times and the 4th it crashes.

Could there be something else that causes this crash?

##### Share on other sites
To avoid this behaviour you can create the stream statically in a function and let the destructor do the work:

void ReadFile(std::string& pfile) {    std::ifstream file(pfile.c_str(), std::ios::binary);    <use file>}

It is recommended to close a file by destruction anyway.
Edit: missed your last post. It seems you already do it like that, or not?

##### Share on other sites
I open the file in a function, like:

void do_something(char * file_name){    ifstream in(file_name, ios::binary);  //crash at the 4th calling    // read file and so on    in.close();    //...};void open_sector(){    //get file name    do_something(file_name);    //...};

So it should be deleted anyway, or not?

##### Share on other sites
ifstream::close() is optional because when you exit the function it is called the destructor and the close method too. However good practice to use it...

The problem is elsewhere...enter in your function via debug and test if your file name string is OK.

##### Share on other sites
Quote:
 Original post by Marco HThanks, but it did not work for me... the strange thing with that error is, that the open-function somehow crashes my program. I tried the clear-function before calling it and I tried to check the good-function, but everything seems fine. Until I try to open the file :(
You say you use "open"-function and tried to "clear" the stream before calling it (i.e. you have some object for which to call the clear, to begin with). But in the code sample you gave, you just open the file in the constructor.. I have a feeling you're showing us different code that you actually use, and this code doesn't break. So we can't help you until you give us the code you actually use.

##### Share on other sites
I have tried the clear-function before opening the file, but it made no difference, so I took away the line in my code. The code above is exactly that what I am using (if you want to see the whole, be prepared to study more than 4000 lines of code ;) ).

@blizzard999:
The file_name string is okay, I have checked that before. Now I am looking if I have left some other files opened, perhaps that could be the reason...

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631424
• Total Posts
3000010
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!