c++ - How to read file from last cursor position? -


char buf[100]; int bufsize = 100; int lastposition = 0; while(!myfile.eof()){ myfile.read(buf,100); myfile.seekg(lastposition); lastposition = lastposition + bufsize; } 

i try read 100 bytes myfile , set cursor position 100. byte. want read 100. byte 200. byte ... till end of file. i'm doing true?

i assume myfile istream.

in case, not way works:

  • read() moves file pointer when reading, no need seek() every time
  • the loop have provided example overwrites buffer each time chunck read, without doing it

you should either try this:

char buf[100]; int bufsize = 100;  while(!myfile.eof()){     myfile.read(buf, bufsize);     // chunck here.         }     

or this:

char * thewholefile; int pos = 0; int chuncksize = 100;   thewholefile = new char[myfile.tellg()];  while(!myfile.eof()){     myfile.read(thewholefile + pos, chuncksize);     pos += chuncksize;  }      // thewholefile contains whole file @ point. don't forget delete @ point! 

i did not handle last chunck here. size can between 0 , 100. can use myfile.tellg() % chuncksize determine actual size (for example).

for further details, have @ related question on stack overflow.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -