std::ifstream must die!

Started by
5 comments, last by klems 20 years, 8 months ago
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."
Advertisement
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).
ASCII stupid question, get a stupid ANSI
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);
// read the junx
fin.rewind();
// read the junx again
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
=-=l33t hax0r? logitank.net
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]
You should use
fin.close(); fin.clear(); together.

This clears any flags (error or otherwise) that were set during reading.
you should also always check to see if the error flags are set, there is a good,failbit,and some others. look them up.
Yes! fin.clear() did the trick! Thanks a lot!

"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."

This topic is closed to new replies.

Advertisement