Using Cygwin on Windows, I wanted to receive a sound notification of specific messages in the server log. I wrote the following:
#include <stdio.h> #include <stdlib.h> int main() { FILE *f = fopen("/dev/stdin", "r"); char bar=' '; if(f==NULL) { return 1; } do { bar = fgetc(f); if((bar=='\n') || (bar=='\r')) { printf("\a"); } if(bar!=EOF) { printf("%c", bar); } } while(bar!=EOF); fclose(f); printf("Done.\n"); return 0; }
Then I ran the following command:
tail -f serverlog | grep myMessage | ./alerty.exe
Sometimes I get notifications, and sometimes not.
My questions are twofold: 1) What is wrong with my C program? Why can't I read the input channel all the time? It aroused my curiosity, so I desperately want to know.
2) How to fulfill the original goal of making my system sound when a certain text is displayed in a file?
c windows-7 pipe cygwin
Ishpeck
source share