B
BartC
Eric Sosman said:[...]
Wrong or not, the Pascal design is far simpler to use. For example, I
can
Simple redeems wrong?
write an outline file-processing loop right now:
while (!eof(f)) {
So we are not at EOF. Well, how *far* are we from EOF? Can we read, say,
512
bytes, without having to bother checking the result?
As soon as we have to check something about the read result, the !eof(f)
check was superfluous.
Aye. There's also the matter of the EOF-test-before-reading
on an interactive device like a keyboard. How would that work,
exactly? "Do you intend to press any more keys? If so, press Y.
If not, press -- oh, wait, if you're not going to press any more
keys you can't press N, can you? Okay: press Y if you're going
to press more keys, otherwise don't press any keys -- and that's
how I'll know whether we're at EOF."
It might work like this:
int c;
while (!eof(stdin)) {
c=fgetc(stdin);
printf("%d %c\n",c,c);
}
eof() is never true, unless perhaps the user presses some special key (eg.
Ctrl D). Which is pretty much how many Unix utilities seem to work.
(Although in my example, the input will also be buffered by the line.)
However ... for known keyboard interaction, you would probably take a
different approach, which might depend on whether you want a
character-at-time interface or a line-oriented one.
But for the million situations where you just have a small, static
line-oriented text file which has a well-defined beginning and end, then my
eof()/readline() loop works perfectly well.
(I can just imagine the hairy compilers the people on here might write which
will be parsing source input even as the programmer is still adding lines at
the end! Sometimes you just have to state the kind of file model that you
want to support, and insist that the files conform to that model.)