[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ctrl+d (EOF) and the standard input.
On Fri, 14 Mar 1997 03:51:34 -0800, you wrote:
>I got an exercise where I have to use ctrl+d (EOF sign, as much as I
>understand) with the standard input that is the channel to input data to
>a C program.
>
>1) Can you explain me what supose to happen ? I mean, when I use ctrl+d
>on the standard input that is passing commands to the shell, I am
>exiting the shell. What supoose to happen when I type ctrl+d when the
>standard input is passing input to a program ?
>
>2)I tried to run the following program:
>
>#include <stdio.h>
>
>int main(void)
>{
> int input_char, num_of_EOFs = 0;
>
> do
> {
> if ((input_char = getchar()) == EOF)
> printf("%d EOF signs were detected so far\n", ++num_of_EOFs);
> } while (input_char != 'q');
> exit(0);
>}
>
>The results were that if I type ctrl+d after a return key, it
>immediately detects the EOF sign. But otherwise I had to press ctrl+d
>twice to make the program understands that I want it to detect an EOF.
>Why is that ?
>
Amos Shapira already sent you an elaborate answer.
I just wanted to add that the above code segment for counting EOFs will
never work because a file can be ended only once.
Once the EOF is detected, all subsequent getchar()'s will also
return EOF. EOF is not another name for the ^D character. Rather,
it is a state of the input stream. There is no sense in counting
EOFs.
--------
Moshe Cohen -- Software Development and Consultancy
17/10 Akiva St., Raanana 43260, Israel.
Tel: 972-9-7489404. Fax: 972-9-7420432.
Email: moshec@netvision.net.il URL: http://www.dezines.com/moshec/
Follow-Ups:
References: