Jump to content
  • Advertisement
Sign in to follow this  
Marco H

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Marco H
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 :(
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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

Sign me up!