Today, I wrote a simple piece of code that uses getchar() to count the characters you input. But when I compile it on Cygwin, it does not work. It always prints 0, but I never input anything or I can’t input any characters it prints 0.
However, if I compile it with VC++6.0, it works.
#include<stdio.h>
int main(void)
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("The total of characters you inputed is %ld.\n", nc);
return 0;
}
This email thread talks about a bug that sounds much like yours, but I can’t see that there are any follow-ups to it.
I would be interested to know what happened when you try
while(getc(stdin) != EOF)and if that doesn’t work, try
while(fgetc(stdin) != EOF)All of them should work, though this page suggests there could be implementation differences between these functions.
Another thing you could try is to print the ASCII value of what you get:
printf("%d\n",(int)getchar());Also, try piping output from a file instead of typing it in the console. Create a file
input.txt, put some characters in it, and docat input.txt | ./programEDIT: You write running cat and piping it works. I would say simply update your Cygwin version. You have encountered a bug. Get the newest versions of Cygwin and the compiler, and you should be good to go. Another option is to use scanf.