1

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 ?

Abhishek Mane
  • 619
  • 7
  • 20
  • Neither are correct. Neither are checking for EOF for the `fin.get`. Dup: https://stackoverflow.com/a/26557243/4641116 – Eljay Aug 27 '21 at 16:02

1 Answers1

4

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.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • what is return type of `fin.get(ch)` ? – Abhishek Mane Aug 28 '21 at 05:59
  • 1
    @AbhishekMane The return value is `fin`, i.e. the same stream that you called the method on. This allows you to chain calls. For example, chain like this - `fin.get(ch).eof()` would let you check `eof` value after read. Or, the better version, `if(fin.get(ch))` will only run if the read was successful. – Yksisarvinen Aug 29 '21 at 14:04