Resetting ifstream

I'm reading lines of text from a file and I need to loop through the entire file twice but I can't figure out how to do it...I have this so far:

1
2
3
4
int main(){
   ifstream dictFile(filename.c_str());
   HashDictionary websta(dictFile);
}


1
2
3
4
5
6
7
8
9
10
HashDictionary::HashDictionary(istream &sin){
	while(!sin.eof()){
		//... do stuff with the lines being read
	}

        // now I want to loop through the file again here but sin.eof() 
        // is already false by this point from the previous loop
	while(!sin.eof()){
		// do stuff
	}
1
2
3
4
5
6
7
8
9
10
while(!sin.eof()){
     //... do stuff with the lines being read
}

sin.clear(); //clear error flags
sin.seekg(0, std::ios::beg); //set the file get pointer back to the beginning

while(!sin.eof()){
     // do stuff
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
HashDictionary::HashDictionary(istream &sin){
	while(!sin.eof()){
		//... do stuff with the lines being read
	}

        // now I want to loop through the file again here but sin.eof() 
        // is already false by this point from the previous loop

        sin.clear() ; // clear the failed eof state **** 
        sin.seekg(0) ; // rewind the stream to the beginning ****

	while(!sin.eof()){
		// do stuff
	}


This is a construct that can introduce a subtle logical error:
1
2
3
while(!sin.eof()){
		// do stuff
	}


Instead prefer doing something like:
1
2
3
while( sin >> key >> data ){
		// do stuff
	}


Topic archived. No new replies allowed.