Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

c - Methods to End Of File (EOF) not working in the NetBeans console

Code Image

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 and 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 .so you have to press enter key + ctrl+z to reach EOF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...