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

Question about EOF

P: n/a
Hi!

I'm wondering why EOF is used in this lines of code (I know this program
won't do very much useful...). Is it a way to say "I want this loop to run
forever", a way to pick up errors, or maybe you can get the EOF value (-1)
from the keyboard to terminate the program?

int c;
while((c = getchar()) != EOF) {
;
}
Kris
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Kris" <no*@vailable.com> wrote in message
news:bu**********@tyfon.itea.ntnu.no...
Hi!

I'm wondering why EOF is used in this lines of code (I know this program
won't do very much useful...). Is it a way to say "I want this loop to run
forever", a way to pick up errors, or maybe you can get the EOF value (-1)
from the keyboard to terminate the program?
There are a few cases:
You can press it on the keyboard. ctrl-z and ctrl-d is what I've seen most
often.
If stdin is redirected from a file you will also get EOF
If an error occurs while reading you get EOF. Check with feof and/or ferror
if an error occurred.
int c;
while((c = getchar()) != EOF) {
;
}
Kris

Nov 14 '05 #2

P: n/a
Kris <no*@vailable.com> scribbled the following:
Hi! I'm wondering why EOF is used in this lines of code (I know this program
won't do very much useful...). Is it a way to say "I want this loop to run
forever", a way to pick up errors, or maybe you can get the EOF value (-1)
from the keyboard to terminate the program? int c;
while((c = getchar()) != EOF) {
;
}


Depending on your OS, you can get the EOF value from the keyboard. Note
that this does not mean that inputting -1 from the keyboard will signal
EOF, but rather that by using an OS-specific way you can get the OS to
close the stdin, which will cause the program to receive EOF. EOF is not
a character but a special condition.
On Unix, EOF is signalled with ^D. On MS-DOS, it is ^Z.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
Nov 14 '05 #3

P: n/a
In <bu**********@tyfon.itea.ntnu.no> "Kris" <no*@vailable.com> writes:
I'm wondering why EOF is used in this lines of code (I know this program
won't do very much useful...). Is it a way to say "I want this loop to run
forever", a way to pick up errors, or maybe you can get the EOF value (-1)
from the keyboard to terminate the program?

int c;
while((c = getchar()) != EOF) {
;
}


It is the canonical way to say "I want this loop to process all the
characters from the program's standard input stream". The actual value
of EOF doesn't matter: getchar will return this value as soon as it
could no longer get characters from the standard input stream (due to
either an I/O error condition or, more often, because it has reached
the end of the stream).

When the standard input stream is connected to the program's console,
the system usually provides a way of generating and end of file condition
from the keyboard. Again, this has absolutely nothing to do with the
actual value of the EOF macro.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.