seekg - c++ reading from beginning of file to a specified byte -


trying write file reads first byte in file byte specified user. need on logic. if file has letters through z , want read , display first 10, example. here's piece of wrote:

  char byte;   infile.seekg(0l,ios::beg);   infile.get(byte);   cout << byte;    for(int = 0; < num; i++);  //num int specified user.   {       infile.seekg(1, ios::cur);       infile.get(byte);       cout << byte;   } 

first problem - semi-colon on end of for() line:

    for(int = 0; < num; i++);     {         ...     } 

what compiler sees this:

    for(int = 0; < num; i++) { /* nothing num times */ }      {         // code run once     } 

so, remove semi-colon.

next, if you're reading bytes in succession, there's no need seek in between each one. calling get() next byte in sequence. remove seekg() calls.

final problem - function calling infile.get() total of num + 1 times. first call before for loop. in for loop, get() called num times (ie. = 0, 1, 2, 3 ... num-1). can fix either changing for loop counter (i = 1; < num; i++) or removing get() call before for loop. in code below i've chosen second way:

void run(int num, istream &infile) {     char byte;      for(int = 0; < num; i++)     {         infile.get(byte);         cout << byte;     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -