问题
I have been learning C from K&Re2. And the above code is what is mentioned in Pg18(Letter counting program), which I ran for confirmation purposes. I tried entering few characters and press ENTER
, but it was not working. Then I heard about CTRL+Z
,CTRL+C
or CTRL+D
with ENTER
for End Of File. I tried it in NetBeans console, but it was not working. I tried \0
and \n
too, pity it was not working too. I have searched for this, but all seemed to have solved the problem with CTRL+Z
,CTRL+C
or CTRL+D
with ENTER
method. I can't understand what is the problem here.
PS: I use Windows 7
Sorry for not inserting code directly. Here is it-
#include <stdio.h>
#include <stdlib.h>
int main() {
long c = 0;
while (getchar() != EOF) {
++c;
}
printf("%ld", c);
return 0;
}
In the image, I have not initialized value of long c
. Sorry for that. This program is running, but the methods I use for EOF doesn't work out.
EDIT:
I have tried compiling in NetBeans, and then running the resulting .exe in cmd rather than in NetBeans console. CTRL+Z
seems to work! Do you guys have any idea why it doesn't work in NetBeans console?
回答1:
getchar()
stores characters in buffer until you press enter key. After enter key is pressed,first character is taken from buffer if no subsequent variable is being assigned.As you used while
loop it will take until \r\n
.so you have to press enter key
+ ctrl+z
to reach EOF.
回答2:
Windows Only
Product Version: NetBeans IDE 8.2 (Build 201609300101)
Updates: NetBeans IDE is updated to version NetBeans 8.2 Patch 2
Run > Set project configuration > Customize...
Category = Run
Console Type = External Terminal
External Terminal Type = Command Window
Click Apply then OK
Run project
To send EOF press ENTER then CTRL+D Or Press CTRL+D twice
来源:https://stackoverflow.com/questions/36052936/methods-to-end-of-file-eof-not-working-in-the-netbeans-console