C program output in wrong order Eclipse

后端 未结 4 680
天涯浪人
天涯浪人 2020-11-29 12:36

I have set up Eclipse for c programming on my Windows machine, I have successfully run a \"hello, world\" program. However, when I try to ask for user input and run the prog

4条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 13:13

    Yeah, Eclipse will buffer a certain amount of output (I don't remember how much off hand) before it will appear in the output window. Eclipse is communicating with the attached process through a pipe which is fully buffered. It won't flush until either fflush() is called or the buffer is full. I found that when debugging with Eclipse, things work best if I put the following near the beginning of my application:

    setvbuf(stdout, NULL, _IONBF, 0);
    

    This will cause stdout to flush immediately whenever it is written to. If you want to use that for debugging and turn it off otherwise, you can conditionally compile it:

    #ifdef DEBUG
    setvbuf(stdout, NULL, _IONBF, 0);
    #endif
    

    No need to put fflush() everywhere this way.

    Edit

    Here's where I found the solution when I first ran into this issue myself.

    http://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows

    Eclipse's console is not a true console or terminal but rather eclipse is communicating with the attached process through a pipe which is fully buffered not line buffered. This is why a newline '\n' does not cause the buffer to be flushed.

提交回复
热议问题