ifstream.seekg failing

B

Brian Ronk

I'm having a strange error. I have a Debian stable server and I'm
using gcc 3.3, but it looks like I have 3.4 installed as well. I
recently made a change to a program that takes data from two files and
converts it into csv format so the final files don't have an extension
of any kind. When I did this, I began having problems with one of my
input files. This file is scanned through, and then it is returned to
the beginning at the beginning of my loop. It looks like the seekg
that I'm using is failing. Here's the line that I'm using:

// stateData is the name of the input file variable
stateData.seekg(0, ios::beg);
// I also tried stateData.seekg((streamoff) 0), but it didn't help.

Unfortunetly, I haven't been doing much programming recently, so I'm
getting a bit rusty, and I dont' know some of the newer features
(namespaces and some more advanced things like that). I've been trying
to figure out what's going on with no success, all I know is that
statData.tellg() is -1 after 1 run through of the file. The first loop
goes fine, and stops in the middle of the file, the next gives me -1.
If anyone can point me in some sort of direction, that would be great.
Thanks.
 
B

Brian Ronk

I just figured it out. For some reason, after I was done searching the
file, I called stateData.close(). Now the question becomes, why the
heck did it work before? It makes sense why it wasn't working now, you
can't go the beginning of a file that's closed. That's a bit hard to
do. Oh well. Thanks anyway.
 
J

Jim Langston

Brian Ronk said:
I just figured it out. For some reason, after I was done searching the
file, I called stateData.close(). Now the question becomes, why the
heck did it work before? It makes sense why it wasn't working now, you
can't go the beginning of a file that's closed. That's a bit hard to
do. Oh well. Thanks anyway.

Easy enough to answer the question "why the heck did it work before?".

Undefined behavior.

Undefined means it can do anything (including looking like it's working).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top