Re-opening closed files?

Hi there, I'm having a problem re-opening files of type ifstream that I closed. for example...

1
2
3
4
ifstream file_in;
file_in.open("data.dat");
file_in.close("data.dat");
file_in.open("data.dat");


After this, the file does not open correctly - file_in.fail() returns true every time. Am I doing anything wrong?

I'm using the g++ compiler.
Last edited on
1
2
3
4
5
ifstream file_in;
file_in.open("data.dat");
file_in.close("data.dat");
file_in.clear();
file_in.open("data.dat");


I think that should work
I just found that on google.. I was just about to post it but oh well. But thank you, that fixed the problem. What does ifstream.clear() do anyways?
resets it. anything you did before .clear has no effect; it's like recalling it.
That's a question that can easily be answered by reading the API on this site:
type "ifstream" into search bar up top.

Jsel wrote:
What does ifstream.clear() do anyways?

http://cplusplus.com/reference/iostream/ifstream/
Topic archived. No new replies allowed.