我使用 win8 Consumer preview build 8250 执行程序,在 win7 上运行正常 该程序使用以下宏/函数:
#if defined(_WIN32)
#include <io.h>
#define streamDup(fd1) _dup(fd1)
#define streamDup2(fd1,fd2) _dup2(fd1,fd2)
#endif
static int acquireOutputStream()
{ int fd = streamDup(fileno(stdout));
FILE* f = freopen("tmp","w",stdout);
return fd; }
static void releaseOutputStream(int fd)
{ fflush(stdout);
streamDup2(fd,fileno(stdout));
close(fd);
}
该程序执行以下操作:
for (int i = 0; i < 1000;++i) {
int fd = acquireOutputStream();
printf("redirect %d\n",i);
releaseOutputStream(fd);
printf("test %d\n",i);
}
每次我运行它时,它都会打印到文件 tmp 随机数的正确“重定向 j”打印:在它之后,对于剩余的执行,文件是空的。(f指针在acquireOutputStream中永远不会为NULL)“test j”总是打印正确。可能是什么问题?这是win 8上的一个已知问题吗?