Question about EOF

K

Kris

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
 
S

Serve Laurijssen

Kris said:
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.
 
J

Joona I Palaste

Kris said:
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 ([email protected]) ------------- 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
 
D

Dan Pop

In said:
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
 

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

Similar Threads

Getchar() problem 8
problem with getchar EOF 2
How to input EOF? 6
EOF question.. 2
Keyboard stroke for EOF ? 4
Determining EOF using fseek()? 6
getchar() and EOF confusion 21
EOF question 41

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top