"siliconwafer" <sp*********@yahoo.com> writes:
I want to know tht how can one Stop reading a file in C (e.g a Hex
file)with no 'EOF'?
The question you're asking doesn't make much sense, but I'm going to
take a guess at what you actually meant.
EOF is an abbreviation for End Of File. Every finite file has an end.
(Some virtual files, such as /dev/zero and /dev/random on Unix-like
systems, are infinite, but I don't think that's what you're referring
to).
EOF is a condition, not a character value. Some systems may use a
special non-printable character to mark the end of a text file (e.g.,
MS-DOS uses character 26, control-Z); this method can't work for a
binary file that can contain any arbitrary character within it. If
you're reading a text file, you'll never see this marker character;
the C I/O functions will translate it to an EOF condition before you
see it.
You refer to a "Hex file". That would be a file containing a sequence
of the printable characters '0'..'9' and 'A'..'F' (or 'a' .. 'f'), but
I don't think that's what you mean. A binary file, which can contain
any arbitrary data, is commonly *displayed* in hexadecimal, but it's
not stored that way, so referring to a binary file as a "hex file" is
incorrect.
On some systems, you have to distinguish between text and binary modes
when opening a file. On others, you don't have to (since text and
binary files happen to behave the same way), but it's a good idea to
do so anyway. Read your documentation for the fopen() function,
particulary the meaning of the second argument ("mode").
The way to read a single character from a file is to use the getc()
function. If "in_file" is a FILE* you've opened with fopen(),
you can call
getc(in_file);
and it will return an int value. If there's a character to be read,
it will return the value of that character as an unsigned char
converted to int; if your system has 8-bit characters (which it almost
certainly does), this will be a value in the range 0..255. If you've
reached the end of the file, getc() will return the special negative
value EOF; since it's negative, it's distinct from any valid character
value. (getc() also returns EOF if there's an error.)
One common error is to store the value returned by getc() in a
variable of type char rather than int. If you do this, you're
throwing away information; for a binary file, some valid character
values will look like EOF. Don't do this.
Another common error is to use the feof() function rather than
checking for EOF (a couple of people suggested this). The feof()
function doesn't tell you that you've reached the end of the file; it
tells you that you've just tried to go past the end of the file. The
feof() function will return true only *after* getc() has returned EOF.
Once getc() returns EOF, the feof() function can be useful for
determining whether you've actually reached the end of the file or
encountered an error; it's not a good way to determine whether there's
anything left to read.
The usual way to read a file (either text or binary) a character at a
time is:
int c;
while ((c = getc(in_file) != EOF) {
/* process a character */
}
The loop will terminate when you reach the end of the file.
Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
Also, study this program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
int main(int argc, char **argv)
{
FILE *in_file;
int c;
unsigned long count = 0;
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
in_file = fopen(argv[1], "rb");
if (in_file == NULL) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
while ((c = getc(in_file)) != EOF) {
count ++;
if (isprint(c)) {
putchar(c);
}
else {
switch (c) {
case '\a':
fputs("[\\a]", stdout);
break;
case '\b':
fputs("[\\b]", stdout);
break;
case '\f':
fputs("[\\f]", stdout);
break;
case '\n':
fputs("[\\n]", stdout);
putchar('\n');
break;
case '\r':
fputs("[\\r]", stdout);
break;
case '\t':
fputs("[\\t]", stdout);
break;
case '\v':
fputs("[\\v]", stdout);
break;
default:
printf("[%02X]", (unsigned)c);
break;
}
}
}
printf("[EOF]\n\n");
printf("Read %lu characters from %s\n", count, argv[1]);
printf("feof(in_file) is %s\n", feof(in_file) ? "true" : "false");
printf("ferror(in_file) is %s\n", ferror(in_file) ? "true" : "false");
fclose(in_file);
exit(EXIT_SUCCESS);
}
--
Keith Thompson (The_Other_Keith)
ks***@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.