-
C code not working
I am dealing with code from The C Programming Language, and can't figure out why it's not working. I believe the program is supposed to count how many characters are in a text stream typed, but whenever you type something, it doesn't output anything. I am running Windows XP, using Dev-C++. Here is the code:
Code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
Does anyone have any ideas of how to fix my code, or what is going wrong? I have tried all my ideas.
-
It won't output anything until it reads EOF. That means you can either run the program, type some junk and then press CTRL-D (or CTRL-Z in Windows), or you can run it like nc < file.txt.
-
Code:
itsme@itsme:~/C$ ./nc
some junk
^D10
itsme@itsme:~/C$ cat file.txt
some junk
itsme@itsme:~/C$ ./nc < file.txt
10
itsme@itsme:~/C$