", both for exit() and for the EXIT_* macros.
exit(0) is equivalent to exit(EXIT_SUCCESS). All of this is portable;
other arguments to exit() can be used in system-specific code.
> Q2. I'm really not confident about the feof() function I used in the
> while loop, I've seen people say you generally don't use feof, anyone
> please tell me somewhat best way to test for EOF?
[...]
Your lack of confidence in feof() is admirable. 8-)}
In your original program, you used both fgetc() and fread(), which was
a large part of your problem. Both functions read input; you should
use just one or the other.
The feof() function is rarely useful. You want to detect when you've
run out of input. The way to do that is to look at the result
returned by whichever input function you're using. The fgetc() and
fread() functions do this differently.
fgetc() returns the next character from the input file (interpreted as
an unsigned char and converted to int). If there is no next
character, it returns the value EOF, which is guaranteed to be
negative and therefore unequal to any unsigned char value. (On some
exotic systems, char and int can be the same size, so EOF could also
be a valid character value; you almost certainly don't need to worry
about that possibility.)
fread() returns the number of items successfully read (note that this
isn't the number of characters unless the size argument happens to be
1). If there are no more items to read, it returns 0; if there are
some items to read, but fewer than you asked for, it returns a value
smaller than what you asked for.
With either function, you can use the result to determine whether it
ran out of data to read. *After* that happens, you can, if you wish,
call feof() and/or ferror() to find out whether this happened because
you reached end-of-file or because of some error.
If you haven't already done so, read section 12 of the comp.lang.c
FAQ, <http://www.c-faq.com/stdio/>.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"