0

I'm learning form a site and i have a task to do. I have read characters from a txt file using >> . That's what i have:

#include <iostream>
#include <fstream>

int main()
{
std::ifstream file( "myfile.txt" );
if( !file.good() )
     return 0;
    char aChar;

do{
    //file.seekg(2, std::ios::cur);
    file >> aChar;
    std::cout << "Char: " << aChar << std::endl;


}while(!file.eof() and !file.fail() and !file.bad());

return 0;
}

There's a comment because i had to read every 3 chars, but that's not the point. Even when i'm reading one char at a time last char always reads twice.

Possible output: 1 2 3 4 4

Text in file: 1 2 3 4

I don't mind other mehtodes, but i'd love to know why that's the case.

Gortax
  • 3
  • 2

0 Answers0