By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

How to stop reading a file?

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

Dec 30 '05 #1
Share this Question
Share on Google+
30 Replies


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


Look up feof().

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Dec 30 '05 #2

P: n/a
siliconwafer wrote:

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/>
Dec 30 '05 #3

P: n/a
On 30 Dec 2005 04:06:09 -0800, in comp.lang.c , "siliconwafer"
<sp*********@yahoo.com> wrote:
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
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 30 '05 #4

P: n/a
Chuck F. wrote:
siliconwafer wrote:

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?


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

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 30 '05 #5

P: n/a
"siliconwafer" <sp*********@yahoo.com> wrote:
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I ASSURE YOU WE'RE OPEN!
Dec 30 '05 #6

P: n/a
can u give more details?

Dec 30 '05 #7

P: n/a
Afifov wrote:
can u give more details?

About what? When you read this group, do you pay attention?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 30 '05 #8

P: n/a
"siliconwafer" <sp*********@yahoo.com> writes:
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
Dec 30 '05 #9

P: n/a
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.

Dec 30 '05 #10

P: n/a
"Afifov" <af****@yahoo.com> writes:
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").

--
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.
Dec 30 '05 #11

P: n/a
Afifov wrote:
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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 30 '05 #12

P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 30 '05 #13

P: n/a
"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.
Dec 30 '05 #14

P: n/a
Afifov wrote:
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/>
Dec 30 '05 #15

P: n/a
Afifov wrote:

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/>
Dec 30 '05 #16

P: n/a
On Fri, 30 Dec 2005 10:28:06 -0500, "Afifov" <af****@yahoo.com> wrote:
can u give more details?


Sure. About what?

--
Al Balmer
Sun City, AZ
Dec 30 '05 #17

P: n/a
On 30 Dec 2005 14:41:06 +0100, in comp.lang.c , Niklas Norrthon
<do********@invalid.net> wrote:
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
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 31 '05 #18

P: n/a
"Chuck F." wrote:

Afifov wrote:
can u give more details?

No. Mr. u is very sick. You will have to ask elsewhere. Can you
provide proper context?

u c m ducks? m r 2 ducks, c m wings???

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Dec 31 '05 #19

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charles Richmond wrote:
"Chuck F." wrote:
Afifov wrote:
can u give more details?


No. Mr. u is very sick. You will have to ask elsewhere. Can you
provide proper context?


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)

iD8DBQFDtgNxagVFX4UWr64RAspCAKCvFktygVdv7anI1TYsP3 5gP/A78gCeOW1H
Ka9N1jMiZhva+o5wXW04yBA=
=kFvx
-----END PGP SIGNATURE-----
Dec 31 '05 #20

P: n/a
Lew Pitcher said:
F U N E X ?
s v f x.
F U N E M?
s v f m.
O K. M N X.


Wonderful. I can just imagine (in my mind's ear, so to speak) someone like
John Cleese reading the upper case lines, and Michael Palin the rest.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 31 '05 #21

P: n/a
Richard Heathfield wrote:
Lew Pitcher said:
F U N E X ?
s v f x.
F U N E M?
s v f m.
O K. M N X.


Wonderful. I can just imagine (in my mind's ear, so to speak)
someone like John Cleese reading the upper case lines, and
Michael Palin the rest.


I haven't the vaguest idea what that is supposed to represent!

V E N I
h u h
V I D I
w h a
V I C I

--
"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/>
Dec 31 '05 #22

P: n/a
Chuck F. said:
Richard Heathfield wrote:
Lew Pitcher said:
F U N E X ?
s v f x.
F U N E M?
s v f m.
O K. M N X.


Wonderful. I can just imagine (in my mind's ear, so to speak)
someone like John Cleese reading the upper case lines, and
Michael Palin the rest.


I haven't the vaguest idea what that is supposed to represent!


You have to read it out loud, letter by letter, in a very British and very
lazy accent. It's a shame to spoil it, but I'll translate the first line
for you.
F U N E X - eff ewe enn ee ex - "'ave you any eggs?"

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 31 '05 #23

P: n/a
Richard Heathfield wrote:
Chuck F. said:
Richard Heathfield wrote:
Lew Pitcher said:

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

Wonderful. I can just imagine (in my mind's ear, so to speak)
someone like John Cleese reading the upper case lines, and
Michael Palin the rest.


I haven't the vaguest idea what that is supposed to represent!


You have to read it out loud, letter by letter, in a very
British and very lazy accent. It's a shame to spoil it, but I'll
translate the first line for you. >

F U N E X - eff ewe enn ee ex - "'ave you any eggs?"


Which, to me, leaves M? Maybe last N.

--
"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/>
Dec 31 '05 #24

P: n/a
Chuck F. said:
Which, to me, leaves M? Maybe last N.


'Am! (As in "eggs n' 'am".)

(An Englishman trying to sound "posh" (when in fact he's not) might well
pronounce "ham" as "'em".)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 31 '05 #25

P: n/a
In article <dp**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com> (Richard
Heathfield), in*****@invalid.invalid says...
Chuck F. said:
Which, to me, leaves M? Maybe last N.


'Am! (As in "eggs n' 'am".)

(An Englishman trying to sound "posh" (when in fact he's not) might well
pronounce "ham" as "'em".)


Obviously he's not watched enough Python. :-)

(and I don't mean the programming language...)
--

-- Len Philpot -> le*@philpot.org <--
------ ><> -----> http://philpot.org/ <--
Dec 31 '05 #26

P: n/a
On Sat, 31 Dec 2005 08:42:56 -0500, in comp.lang.c , "Chuck F. "
<cb********@yahoo.com> wrote:

Which, to me, leaves M? Maybe last N.


M, part of a pig, commonly prepared by curing, smoking and slicing.

N, preposition commonly found in pub names such as the one referring
to a dog plus a duck.
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 1 '06 #27

P: n/a
Len Philpot said:
In article <dp**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com> (Richard
Heathfield), in*****@invalid.invalid says...
Chuck F. said:
> Which, to me, leaves M? Maybe last N.


'Am! (As in "eggs n' 'am".)

(An Englishman trying to sound "posh" (when in fact he's not) might well
pronounce "ham" as "'em".)


Obviously he's not watched enough Python. :-)


Yes, one cannot help but imagine John Cleese in the starring role here. But
of course John Cleese has a perfectly "posh" voice when he chooses to use
it, which makes me realise that he was often in the position - whilst
Pythoning, I mean - of an articulate man pretending to be a
semi-inarticulate man pretending to be an articulate man.

Now, imagine a Cockney "chirpy sparrer" trying to imitate John Cleese saying
"F U N E X"... and now imagine John Cleese trying to imitate that
Cockney... and now imagSegmentation fault (core dumped)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 1 '06 #28

P: n/a
"Chuck F." wrote:

Richard Heathfield wrote:
Lew Pitcher said:
F U N E X ?
s v f x.
F U N E M?
s v f m.
O K. M N X.


Wonderful. I can just imagine (in my mind's ear, so to speak)
someone like John Cleese reading the upper case lines, and
Michael Palin the rest.


I haven't the vaguest idea what that is supposed to represent!

V E N I
h u h
V I D I
w h a
V I C I

Just like trying to read the OP when he is posting all of his
ur i type drivel...

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Jan 1 '06 #29

P: n/a
Groovy hepcat Richard Heathfield was jivin' on Sat, 31 Dec 2005
05:48:41 +0000 (UTC) in comp.lang.c.
Re: How to stop reading a file?'s a cool scene! Dig it!
Lew Pitcher said:
F U N E X ?
s v f x.
F U N E M?
s v f m.
O K. M N X.


Wonderful. I can just imagine (in my mind's ear, so to speak) someone like
John Cleese reading the upper case lines, and Michael Palin the rest.


I believe it was The Two Ronnies, if memory serves.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Jan 4 '06 #30

P: n/a
Peter Shaggy Haywood said:
I believe it was The Two Ronnies, if memory serves.


You're too late. I now have the whole thing firmly rooted into my fake audio
memory - half in a John Cleese accent and the other half mumbled into a
handkerchief by J Random OtherPythoner.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 4 '06 #31

This discussion thread is closed

Replies have been disabled for this discussion.