• Announcements

Archived

This topic is now archived and is closed to further replies.

std::ifstream must die!

Recommended Posts

Valderman    513
I''m making a simple text parser class for the scripting language I''ve been working on for a while. I''ve never done anything with text parsing before (at least not as advanced as this compiler-like thingy), so I expected to get stuck pretty fast, which happened. Although not in the way I expected. I have a
std::ifstream
as a private member of this class, and at the start of the main parsing function, I open a file with it, loop through the entire file, do some stuff with the contents and then close it again. Now when I want to reopen the file, it just won''t work. The reopening code looks like this:
fin.close();
fin.open(file);

The file is closed and then reopened, nothing inbetween, and the filename is the same, both in this second call to open, and in the open() at the beginning of the function. Why does it do this? It doesn''t make sense to me.
"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!" "If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

Share on other sites
Yohumbus    152
Well I don''t know that seems pretty odd to me.. What I would suggest though is look up the docs for the member seekg which should allow you to just go back to the beggining of the file(which it looks like you are trying to do).

Share on other sites
wunderk1nd    122
If you are just trying to seek to the beginning of the file you have opened, you could look up seekg, or you could just use the rewind function.. like so

ifstream fin;
fin.open(blahblahblah);
fin.rewind();
fin.close();

If you were trying to open a whole new file... i dunno. You could try a few things, like getting rid of the call to close and just have the open.

Good Luck

Share on other sites
Valderman    513
I already tried to seekg to the beginning of the file, and it didn't work either...quite odd.
I'll have to look at that rewind() method. Thanks!

EDIT: Nope, the rewind() method appears to be nonexistant, at least on VC.NET.

"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"
"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately ."

[edited by - Valderman on August 7, 2003 1:32:16 PM]

Share on other sites
Guest Anonymous Poster
You should use
fin.close(); fin.clear(); together.

This clears any flags (error or otherwise) that were set during reading.

Share on other sites
deathtrap    364
you should also always check to see if the error flags are set, there is a good,failbit,and some others. look them up.