Search code examples
c++getfile-handlingifstreameof

`fin.get(ch);` reads last character twice while `ch=fin.get();` not inside `while(!fin.eof())` loop why?


vicky.txt file

I was born in Hupari.

Code -1

#include <iostream>
#include<fstream>
int main()
{
    char ch;
    std::ifstream fin;
    fin.open("vicky.txt", std::ios::in);

    while (!fin.eof())
    {
        fin.get(ch);
        std::cout<<ch;
    }
    fin.close();
    return 0;
}

Output

I was born in Hupari..

Code -2

#include <iostream>
#include<fstream>
int main()
{
    char ch;
    std::ifstream fin;
    fin.open("vicky.txt", std::ios::in);

    while (!fin.eof())
    {
        ch=fin.get();
        std::cout<<ch;
    }
    fin.close();
    return 0;
}

Output

I was born in Hupari. 

Why while using fin.get(ch) it reads last character twice. On other hand ch=fin.get() reads correctly means reads last character one time only.

By the way also tell return type of fin.get(ch) ? and return type of fin.get() is char right ?


Solution

  • Both versions are wrong. See Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong? for more details on how to correctly code reading loops.


    Now, to the explanation what happens. The first version uses second overload of get(). This method leaves its argument unchanged if read fails (e.g. when end of file is reached) and sets flag. Thus, you print the last read character once again.

    The second version uses first overload of get(). Because this version must return a character and cannot leave it unchanged like the other versions, it returns an int to be able to return special value EOF (which must not represent any valid character). EOF is typically equal to -1 (as int). Then, you implicitly cast it to char, which would make it equal to 255 (or keep it as -1, but 8 bits). You then print it. Depending on the code page used by the terminal, it could be ÿ char, non-breaking space or something else. Possibly, you have that non-breaking space character printed, or some other invisible or unprintable character.