为什么 tee 会等待 .exe 程序结束?

Why does tee wait the end of the .exe program?

我在安装了 cygwin 1.7.0 的 Windows 7 主机上。

我想在控制台上看到 myTest.exe 的输出,同时将其写入 myTest.log,但所有输出仅在 myTest.exe 结束时显示。

我尝试了 here 建议的解决方案,效果很好。然后我写了下面的myTest.c:

#include <stdio.h>
#include <Windows.h>

int main() {
    printf ("hello\n");
    Sleep(5000);
    printf("goodbye\n");
    return 0;
}

并用命令编译

gcc myTest.c -o myTest

在没有 tee 的情况下执行 test.exe 按预期工作,但如果我执行

./myTest.exe | tee myTest.log

只有在 myTest.exe 完成后,我才能在控制台上获得所有输出。

关于如何在 myTest.exe 仍然是 运行 时将输出发送到控制台的任何建议?

控制台输出被缓冲,所以当程序退出时,它会刷新缓冲区。您需要在睡眠之前明确刷新缓冲区,以便立即写入。例如:

fflush(stdout);