tags:

views:

417

answers:

5

I'm reading "The C Programming Language (2nd ed.) and near the beginning, it has examples like this:

while((c = getchar()) != EOF)
    if(c == '\n'){
        ++n1;

I can see how this would work while reading from a file, and I understand this syntax... But this is just reading from the console--how does one signal end of file when entering characters from a console? I'm using Windows XP... MinGW compiler... Anyways, was this book written for waaay earlier systems with like an EOF button or something?

Update

well, I have one more question, that's just related to how the end-of-file works under Windows.

If I just while(getchar()!=EOF);, then I can just keep typing characters until I signal EOF via ^Z. But, I have to write a newline, then hit ^Z, then another newline... Why does it have to be on its own line?

+4  A: 

^Z is EOF.

danbystrom
+8  A: 

Windows uses Ctrl-Z for EOF, and UNIX uses Ctrl-D. See http://bytes.com/groups/c/217873-eof-windows , and excellent book choice. :)

Matthew Flaschen
Thanks! Haha and I'm getting it for my birthday, but I couldn't wait so I downloaded it and am having a peek. But the OCR read some words and letters so wrong, and some pages won't load, so it's not a 100% awesome experience, and I feel like I'm missing a lot of information.
Carson Myers
Well, try to re-read it without the OCR glitches.
Matthew Flaschen
I have to wait about a month, that's all.
Carson Myers
+3  A: 

The correct answer has been already given, but a typical usage would be to redirect a file to standard output:

program.exe < samplefile.txt

samplefile.txt is "written" to standard out and program.exe reads this from standard out until the EOF is reached.

0xA3
cool, I'll try that too
Carson Myers
Just to be clear - there is no EOF "in the file" for text files - it is provided by the OS on reading past the end of the text in the file.
anon
@Neil: Thanks for the clarification, you are totally correct. I updated the wording to avoid misunderstanding.
0xA3
+1  A: 

Regarding your question on ^Z, the reasonn it behaves like this is because it isn't really a character, it's a signal from the operating system to the C input system. As such, it is highly dependant on the interaction between the OS and the C input system buffering. Which is a fancy way of saying that it's just the way things work, for Windows and for your particular C implementation.

anon
ah, alright, thanks
Carson Myers
A: 

write for me EOF in full.

turinawe nicholas