ifstream "reset"

Hello,
I can't find anywhere what the right way of writing this is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	ifstream arx;
	char pass[9];
	int i;
	
	arx.open("loginfile.txt");
	
	for (i=1; i<=4; i++){
	arx >> pass;
	cout << "pass" << i << ": "<< pass << endl;}
	
	cout << endl;
	
	for (i=1; i<=4; i++){
	arx >> pass;
	cout << "pass" << i << ": "<< pass << endl;}
	
	arx.close();


(loginfile.txt has four lines)

And the output is:
pass1: passwor1
pass2: passwor2
pass3: passwor3
pass4: passwor4

pass1:
pass2:
pass3:
pass4:


So, I want multiple readings on that file but the problem is that after the first reading, the "arx" reads below the point it stopped the first time.
How can I solve that?

I tried putting
1
2
arx.close();
arx.open("loginfile.txt");

between the two loops, thinking that it may "reset" the arx but it didn't work.

I also did it with two ifstreams, (ifstream.arx and ifstream.arx2) and it worked, but it doesn't seem to me that creating separate arx's everytime I want to read it is the right (the most practical) way of doing it.

Thank you in advance, and excuse me any mistakes in the way of expressing myself as I'm a noob in c++ and a non-native english speaker.
Last edited on
You know the reference section on this site has helped me out more then I can even think about.

http://www.cplusplus.com/reference/iostream/istream/seekg/

In your case I believe arx.seekg(0, ios::beg); right around line 6 in your sample up there should do wonders for you.

EDIT: Forgot the ';'
Last edited on
It works, thank you so much!

The reference section is very helpful but my noobidity prevented me from searching with the right terminology and I couldn't find the answer.

Thank you again!
Topic archived. No new replies allowed.