【发布时间】:2019-04-13 19:15:25
【问题描述】:
我有一个 C 代码。
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
int a = 1;
while( a <= 5 )
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("Normal prinf funcation call from C\n");
fprintf(stdout, "STDOUT, Got on STDOUT from C. - now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
fprintf(stderr, "STDERR, Got in STDERR from C. - now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
sleep(1);
a ++;
}
return 0;
}
在 Linux 上 我用 gcc 编译这个 C 代码。生成一个二进制文件。
当我执行二进制文件时,我看到以下输出;
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:38
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:38
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:39
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:39
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:40
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:40
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:41
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:41
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:42
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:42
在 windows 机器上,使用 cygwin 和 gcc,我将相同的 C 代码编译成 .exe 文件,然后尝试运行它在 cmd 中(不是 cygwin,适用于 cygwin)。屏幕上没有打印任何内容。
Linux 和 Windows 上的 STDOUT/STDERR 有什么主要区别吗?
如何使 .exe 文件打印到命令提示符(至少 printf 调用应该有效。)?
P.S:我在 Linux 和 Windows 上都使用以下命令来生成二进制/exe。
gcc C_code.c -o binary
【问题讨论】:
标签: c linux windows operating-system