Ifstream problem

Hello :)

I'm trying to re-read a file multiple times, but something goes wrong. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>
using namespace std;


int main(){
	
	ifstream file("file.txt");
	string str;

	while(1){

		while(getline(file,str)){
			cout<<str<<endl;
		}

		str="";
		file.seekg(0);
	}

	return 0;
}


I thought line 18 or 19 would solve the problem, but the problem is not there.
If I replace line 18 and 19 with file.close(); file.open("file.txt") it works and it prints the content of the file infinitely.
So, where is the problem ?
Thanks in advance :)
At the end of this loop while(getline(file,str)), the stream is in a failed (and eof) state.

Clear the failed state first with file.clear(), http://www.cplusplus.com/reference/ios/basic_ios/clear/
and then seek to the beginning.
Thank you, it worked :)
Topic archived. No new replies allowed.