c++ - ifstream - Read last character two times -


when reading chars textfile dont know why last character read 2 times? if insert new line row no longer read 2 times.

heres class

class readfromfile {  private:     std::ifstream fin;     std::string allmoves;  public:     readfromfile(std::string filename) {          fin.open(filename, std::ios::in);          char my_character;         if (fin) {             while (!fin.eof()) {                 fin.get(my_character);                 allmoves += my_character;             }           } else {             std::cout << "file not exist!\n";         }          std::cout << allmoves << std::endl;     } }; 

and heres content of textfile (without newline)

 1,2 3,1 1,3 1,2 1,4 

and output:

 1,2 3,1 1,3 1,2 1,44 

you need check fin after fin.get. if call fails (as happens on last char) keep going, despite stream being on (and my_character invalid)

something like:

fin.get(my_character); if (!fin)     break ; 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -