How to stop reading a file?

S

siliconwafer

Hi All,
I want to know tht how can one Stop reading a file in C (e.g a Hex
file)with no 'EOF'?
 
C

Chuck F.

siliconwafer said:
I want to know tht how can one Stop reading a file in C (e.g a
Hex file)with no 'EOF'?

Where did you get such a big disk drive? Who put the endless file
on it? What software did they use. Where did they get the data to
write to the file?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
M

Mark McIntyre

Hi All,
I want to know tht how can one Stop reading a file in C (e.g a Hex
file)with no 'EOF'?

Stop calling fread() ? close the file? :)


Seriously however, read up on feof().
Mark McIntyre
 
N

Niklas Norrthon

siliconwafer said:
Hi All,
I want to know tht how can one Stop reading a file in C (e.g a Hex
file)with no 'EOF'?

Reading a file is not addictive, so it's just a matter of not
reading any more from it.

Btw, what do you mean with "with no EOF"? I can think of a few
examples, like /dev/zero and /dev/random on unix systems, is that
what you mean?

/Niklas Norrthon
 
E

Eric Sosman

Chuck said:
Where did you get such a big disk drive? Who put the endless file on
it? What software did they use. Where did they get the data to write
to the file?

He's probably writing a quality-control test program
to make sure /dev/zero contains only zeroes ;-)
 
S

SM Ryan

# Hi All,
# I want to know tht how can one Stop reading a file in C (e.g a Hex
# file)with no 'EOF'?

The system should report to stdio the end of a disk file and stdio
reports that with EOF or NULL returns or feof. For a file like a
serial port or keyboard, you may have to define your own protocol
within the file to work the end.
 
A

Afifov

Dear Joe,

I do read in this forum.But i believe that there is a couple of ways to go
around reading a file without the use of eof, either by seeking to end of
file through lseek, or getting size of the file and decrementing a counter
(which would be initialized to file size) until it hits zero.
It depends on the task.

But thanks for the tip.
 
K

Keith Thompson

Afifov said:
can u give more details?

Please do one of three things:

1. Find out how to provide proper context in followups using whatever
Usenet interface you're using. The way to do this for Google is
described in detail at <http://cfaj.freeshell.org/google/>, but I have
no idea how to do it through www.talkaboutprogramming.com.

OR

2. If that's not possible, switch to a different Usenet interface. A
proper NNTP server would be ideal, but groups.google.com is free, and
can be used properly with a little effort (see the URL above).

OR

3. Stop posting here.

I'm not seriously suggesting alternative 3, but followups without
context are quite annoying. Many of us do not have easy access to the
parent article. Each followup should be readable by itself. I have
no idea what you're asking for more details about; if you want
information, it's *your* job to make yourself easy to understand.

In addition to that, please write proper English (capitalize properly
and don't use silly abbreviations like "u" for "you").
 
C

Chuck F.

Afifov said:
can u give more details?
No. Mr. u is very sick. You will have to ask elsewhere. Can you
provide proper context?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
J

Joe Wright

Afifov said:
Dear Joe,

I do read in this forum.But i believe that there is a couple of ways to go
around reading a file without the use of eof, either by seeking to end of
file through lseek, or getting size of the file and decrementing a counter
(which would be initialized to file size) until it hits zero.
It depends on the task.

But thanks for the tip.
You missed my point. It was that you did not include any context in your
post. If I can't see previous posts, I have no idea what you are talking
about.
 
R

Richard Heathfield

siliconwafer said:
Hi All,
I want to know tht how can one Stop reading a file in C (e.g a Hex
file)with no 'EOF'?

Keep reading until you receive an EOF message from your file-reading
routine.

For example:

#include <stdio.h>
int main(void)
{
int ch;
while((ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}

Of course, stdin is a great example of a file that doesn't have an EOF
marker. So are most files, actually.

You're probably thinking of the ^Z character (ASCII 26) that marks the end
of some text files in CP/M and MS-DOS and Windows. C doesn't recognise this
character as being particularly special (although your operating system
might).

C's EOF has nothing to do with all that, and is *not* a character. Rather,
it's a message to your program that there's nothing left to read, which you
get when you try to read past the end of the file.

So - to read an entire file with no EOF character, just keep reading until
you hit EOF.
 
K

Keith Thompson

siliconwafer said:
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);
}
 
C

Chuck F.

Afifov said:
I do read in this forum.But i believe that there is a couple of
ways to go around reading a file without the use of eof, either
by seeking to end of file through lseek, or getting size of the
file and decrementing a counter (which would be initialized to
file size) until it hits zero. It depends on the task.

However you consistently ignore the instructions given you on
proper posting, with context. Since you refuse to cooperate, I
think it is about time to start applying the PLONK cure, which
means that people simply won't see, nor respond to, any of your posts.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
M

Mark McIntyre

Btw, what do you mean with "with no EOF"?

He's probably reading a file and expecting to find an Ctrl-Z character
in the stream, like one used to get with the old MSDOS text file
structure. Crtl-Z seems to be called EOF in some character maps. Old
DOS compilers would stop reading a text when they encountered a
Ctrl-Z, even if the file table said the file had more data, and
conversely wouldn't stop if the file didn't have a Crtl-Z, even if the
'more data' was junk.
Mark McIntyre
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charles said:
u c m ducks? m r 2 ducks, c m wings???

F U N E X ?
s v f x.
F U N E M?
s v f m.
O K. M N X.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDtgNxagVFX4UWr64RAspCAKCvFktygVdv7anI1TYsP35gP/A78gCeOW1H
Ka9N1jMiZhva+o5wXW04yBA=
=kFvx
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top