Programming/C++
seekg(0, ios::beg) doesn't work!
Starrysky1101
2010. 11. 28. 02:08
C++에서 ifstream으로 파일을 읽다가 다시 그 위치를 처음으로 돌리고 싶을때
가끔 is.seekg(0, ios::beg); 이게 먹히지 않는 경우가 발생한다.
처음에는 결국 해결 방법을 못 찾아서 파일을 닫고 다시 열어서 읽었던 기억이있다.
혹시나 다음과 같은 경우라면 해결 방법이 있다.
다음-> 파일을 끝까지 읽어 EOF(End Of File) 까지 간경우 bad() state 이기 때문에 clear()를 seekg 전에 써줘야 한다.
- data.clear();
- data.seekg(0);
Since you've gone till the EOF, the stream is now in a bad() state. You need to clear() it before you can seekg():
Reference : http://www.gamedev.net/community/forums/topic.asp?topic_id=447865
가끔 is.seekg(0, ios::beg); 이게 먹히지 않는 경우가 발생한다.
처음에는 결국 해결 방법을 못 찾아서 파일을 닫고 다시 열어서 읽었던 기억이있다.
혹시나 다음과 같은 경우라면 해결 방법이 있다.
다음-> 파일을 끝까지 읽어 EOF(End Of File) 까지 간경우 bad() state 이기 때문에 clear()를 seekg 전에 써줘야 한다.
- data.clear();
- data.seekg(0);
Since you've gone till the EOF, the stream is now in a bad() state. You need to clear() it before you can seekg():
Reference : http://www.gamedev.net/community/forums/topic.asp?topic_id=447865