"Jim" <> wrote in message
news:dd81c729-01d2-436a-ad00-...
> Hi There,
>
> I'm trying to read a file character by character. When I write the
> file out, there is one extra character which shows on the screen as a
> solid circle with a small question mark in the middle.
>
> Here is what I have:
>
> infile = fopen("encrypted.txt", "r");
> outfile = fopen("plain.txt", "w");
>
> while(!feof(infile)) {
That's the wrong way to use feof() as has been explained.
I found it helpful myself to think of feof() as being named feof_triggered()
instead. So it's not true until an attempt is made to read past EOF.
I also made my own feof() which works the way you and I might expect, but
that involves a lot of bad code using fseeks, ftells and assorted
assumptions, so I won't post it here!
> ch = fgetc(infile);
ch returns an EOF value (usually -1) at the end of file. That's what you
should be looking for. I revised your loop to:
while(1)
{ ch=fgetc(infile);
if (ch==EOF) break;
fputc(ch, outfile);
};
--
Bart
|