4.2bsd eof flag in stdio
Geoff Kuenning
geoff at desint.UUCP
Tue Nov 13 17:13:04 AEST 1984
In article <1697 at ucf-cs.UUCP> goldfarb at ucf-cs.UUCP (Ben Goldfarb) writes:
>Why did Berkeley change stdio such that typing ^D (or whatever EOF character
>one is using) on stdin causes that stream to reflect eof until a clearerr()
>is done? Has this been discussed here before? If so, I apologize for
>further belaboring the issue.
>
>In any case, what is the correct approach to this problem?
We did this when I was at DEC because that's the way a file behaves, and it
is frequently easier to write a program to read the EOF twice. For example:
while ((ch = getchar ()) != EOF)
switch (ch)
{
case '\\':
switch (ch = getchar ())
{
case EOF:
break;
}
break;
}
Here, reading the EOF twice is a convenient way to handle the loop exit.
(Yes, there are other ways, notably using a goto. But in more complex code
this approach may be the cleanest). I never like assuming that I can unget
an EOF character (although it works on some systems).
One can also make a persuasive argument for the advantages of the other
approach, but I prefer this way because of consistency.
--
Geoff Kuenning
First Systems Corporation
...!ihnp4!trwrb!desint!geoff
More information about the Comp.unix.wizards
mailing list