Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have written a program in C and have compiled it using MinGW. When I try to run that program in Cygwin, it seems to behave normally (i.e. prints correct output etc.)

However, I'm trying to pipe output to a program so that I can parse information from the program's output. However, the piping does not seem to be working in that I am not getting any input into the second program.

I have confirmed this by using the following commands:

This command seems to work fine:

./prog 

Performing this command returns nothing:

./prog | cat

This command verifies the first:

./prog | wc

Which returns:

0 0 0

I know that the script (including the piping from the program) works perfectly fine in an all Linux environment.

Does anyone have any idea for why the piping isn't working in Cygwin? Thanks!

share|improve this question
    
Do you get the same behaviour piping it to more or redirecting stdout to an actual file? –  Breakthrough Jul 8 '11 at 16:40
    
@Breakthrough Yes. Outputting to a file "./prog &> output.txt" or "./prog > output.txt" yields an empty output.txt –  KLee1 Jul 8 '11 at 16:45
    
a few questions: are you printing to stdout? are you printing the right end of line character for windows (\r\n)? –  Pablo Castellazzi Jul 8 '11 at 21:17
    
@Pablo Castellazzi I am printing to stdout (just using printf), but am using "\n" instead of "\r\n." I don't think that should change the fact that I'm not getting any output at al, right? I will try changing the line endings to \r\n and let you know. –  KLee1 Jul 8 '11 at 21:23
    
I just wrote a little C app that uses printf and was able to pipe to wc and less and such. What version of cygwin are you running? –  Robb Jul 8 '11 at 22:15

1 Answer 1

Most probably this has to do with stdio buffering, as identified and explained previously in the comments section. In my MinGW projects, I have often used the following construct:

#define disable_stdout_buffering() do { \
               setvbuf(stdout, NULL, _IONBF, 0); } while(0)

int main(int argc, char *argv[]) {
    disable_stdout_buffering();
    /* Your code */
    exit(EXIT_SUCCESS);
}

Of course you can also add a call to fflush() in a preprocessor macro or when you output your data to stdout, however this way it does not add an additional library call to each buffer written to your console.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.