c++ - Read file from position (to position) -


file-stream/ifstreamreadbufferandseekfileposition.htm">http://www.java2s.com/tutorial/cpp/0240_file-stream/ifstreamreadbufferandseekfileposition.htm

this page able print content of file twice using

file.seekg(0); 

so assume seekg(0) puts "cursor" (in java) beginning of file, , web site: http://www.cplusplus.com/doc/tutorial/files/ confirms me.

but when file contains (0\n0\n0\n):

while(getline(file,line)) {     cclog(line.c_str());     cclog("%d",(int)file.tellg()); }  file.seekg(0); cclog("%d",(int)file.tellg());  while(getline(file,line)) {     cclog(line.c_str()); } 

(i'm doing in cocos2dx 2.2.2 way)

it prints:

0 2 0 4 0 6 -1 

i don't know i'm doing wrong. please help

the reason behaviour seeing that, once read end of file, eof() state flag set. attempt read or position file after trigger error. error resulting in return code of error, -1.

you can reset state flags (all of them) using clear(). code should read

file.clear(); file.seekg(0); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -