The eof()
check won't return true once you've read the last character; it stays false until after you've tried to read past the end. So rather than checking for eof in the while-loop condition, check for it right after you read (but before you write), and then break.
(Incidentally, a bit of explanation: ÿ is the ANSI character representation of the value 0xFF, which is to say, -1. This is what get()
returns to signal EOF. So if you wanted, instead of checking eof()
, you could see whether the char equalled -1.)