ifstream opening file causes crash

Started by
11 comments, last by Marco H 19 years, 3 months ago
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
Advertisement
std::ifstream::clear()
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?
Which compiler/standard library implementation are you using? IIRC, older service packs of MSVC 6 could cause this kind of problem.
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?
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?
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?
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.
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.
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...

This topic is closed to new replies.

Advertisement