# ifstream opening file causes crash

This topic is 4803 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...

##### Share on other sites
What kind of crash is it anyway? An unhandled exception? An access violation?
What is the state of the program at the instant of the crash? Have you traced it in the debugger?

##### Share on other sites
Quote:
 Original post by Marco HI open the file in a function, like:*** Source Snippet Removed ***So it should be deleted anyway, or not?

"Deleted"?????? I hope you just weren't paying attention to what you were writing, but if that is really what you meant: 1) Deleting the file you are trying to read would obviously cause problems, or 2) Deleting the non-dynamically allocated variable in could have disastrous results; you should just be allowing it to go out of scope and be deleted by the compiler.

Assuming that you don't have a line like
delete ∈ // I hope you don't have this.
I would check file_name. Do you really know for sure that do_something() is being passed the same file_name every time? If file_name is somehow corrupted after the third call of do_something, then that would explain do_something crashing. I would recommend searching (in the code, with the debugger, and by adding extra diagnostic outputs) to find out if file_name is altered anywhere it shouldn't be. The very first thing you should do is make sure file_name is not being changed in do_something() itself. To do this, change the function declaration from
void do_something(char * file_name)
to
void do_something(const char * file_name)
file_name should be a constant in do_something() because there is no legitimate reason for file_name to be changed in do_something() and if you make it a constant the compiler will tell you if it is being changed for some reason. Good luck.

EDIT: I just saw that someone has already asked about file_name. Unless you check file_name right before you open the file each time, it might still be worth looking into. However, even if you feel that it is not worth the time, at least change
void do_something(char * file_name)
to
void do_something(const char * file_name)
because that will only take a moment and that is how it should have been from the beginning anyways.

##### Share on other sites
It is an access violation. It seems like I solved my problem by finding an unclosed file (open_sector() also had opened a file and I did not close it before calling do_something(), perhaps that gave the error), but right now I have got another strange error while allocating some memory.

The line
num_materials = new word;
crashes my program (now the 5th time I reload the sector). I guess I corrupt the heap somewhere, so the program crashes at a certain point, as the crash-causing line is only a clean allocation. Perhaps I should try some sort of memory-leak finder?

@mumpo:
I did not mean to "delete ∈ //never ever", just to let it go out of scope. The file_name is okay, but your tip with the const char's is good. I saw it before, but never took it for that important. Thanks :)

[Edited by - Marco H on January 1, 2005 6:58:02 PM]