Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding start of file. 1

Status
Not open for further replies.

vivasuzi

Programmer
Jun 14, 2002
183
I'm using an input file, and I want to always start from the beginning of the file everytime I open it. My prob is this. I list the contents of a file. It works, i close the file. Later I need to list the contents again, but it's the eof.
I tried--> input.seekg(0,ios::beg);
But when i do this right after--> cout<<endl<<input.tellg()<<endl;
it outputs -1.

No cfile stuff. I just need to be able to read the same input file, from the start, over and over. thanx in advance. *Suzanne*
 
Do you ever reopen the file after you close it?

If you do, it should start reading from the beginning. You'd use seekg if you still had the file open and wanted to go back to the beginning.
 
I had the same problem with my output function the other day! It drove me nuts, but after some experimentation, I discovered that after eof if reached, the eofbit and failbit are set.

It seems like you may be having the same problem. Use cin.clear() after reaching eof, then see if you can reset the pointer.
 
That's what I had to do, I had to clear. I didn't want to do input.clear() b/c I thought that meant clear the contents of the file, but now I understand that it clears the bits. Thanx. *Suzanne*
 
You might want to consider reading the file into an array if it is not too long since that would be more efficient than reading it off of the disk a lot of times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top