Windows8 - _dup, _dup2

I am using win8 Consumer preview build 8250 to execute a program that works fine on win7 The program uses the following macros / functions:

#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);
}

      

The program does the following:

for (int i = 0; i < 1000;++i) {
   int fd = acquireOutputStream();
   printf("redirect %d\n",i);
   releaseOutputStream(fd);
   printf("test %d\n",i);
}

      

Every time I run it, it prints a random number of correct "j redirects" to the tmp file: After that, the file is empty for the rest of the executions. (The F pointer is never NULL in the getterOutputStream). "Test j" always prints correctly. What could be the problem? Is this a known issue on Win 8?

+3


source to share


1 answer


There is one small problem I see with your code.

static void releaseOutputStream(int fd)

{   fflush(stdout);
    streamDup2(fd,fileno(stdout));
    close(fd);
}

      



In this function, you do not close stdout until dup2 (fclose (stdout)) is called.

Please add more details about what exactly you see when you run this code. This will help diagnose the problem.

0


source







All Articles