Alf has already given you the technical answer. Your confusion arises
because you made a false assumption about the state of in. Here is
one way to analyze the situation should you run into something similar
in the future. It falls under the general heading of "pretend you are
the computer".
Consider the sequence if the file contains two characters, '8' and
'9'.
You open the file. The ifstream object in now corresponds to that
file.
The while expression evaluates to true.
You read the character '8'. The operation completes normally.
You write the contents of ch ('8').
The while expression evaluates to true.
You read the character '9'. The operation completes normally.
You write the contents of ch ('9').
The while expression evaluates to true.
You attempt to read another character. There is none available.
The operation fails. Object ch is unchanged.
You write the contents of ch ('9').
Now the while expression evaluates to false and you close the file.
Reading the last character of a file and no further does not set "end
of file" status. That only happens when you attempt to read beyond
the last character.
--
Remove del for email