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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ifstream::seekg problem

Status
Not open for further replies.

globos

Programmer
Nov 8, 2000
260
FR
Hi,

I am using the STL and have a problem with the feature ifstream::seekg.
I want to read twice a file, the first read is ok, and after that I want to go back to the beginning of the file, in order to perform the second read.

Here is a code snippet:

#include <fstream>
#include <assert.h>
...
ifstream ins (&quot;afile.txt&quot;);
assert (ins.is_open ());
while (not ins.eof ())
{
//read the file ...
}
ins.seekg (0, ios::beg);//go back to the beginning
assert (not ins.eof ());//fails
...

I don't clearly understand the first parameter of seekg, so I think I use it in a wrong way.
Can somebody tell me what is wrong?

Thanks

--
Globos
 
sorry, if you reach eof you should clear state:
ifstream ins (&quot;salut.txt&quot;);
assert (ins.is_open ());
while (! ins.eof ())
{
ins.get();
//read the file ...
}
ins.clear();
ins.seekg (0);
assert ( ! ins.eof ());//

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Thank you for that quick answer.

now it works!

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top