Question

I have wrote the following code:

int fd = _dup(fileno(stdout));
FILE* tmp = freopen("tmp","w+",stdout);
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
if (out == INVALID_HANDLE_VALUE){
      //error
}
else if (out == NULL) {
      //error
}
else {
   WriteFile(out, "num", sizeof("num"), NULL, NULL);
 }

In the last line I get an assertion "Unhandled exception...:Access violation writing location 0x000000" What could be a problem and the fix for it?

Thank you.

P.S:Due to limitation of the project I can`t use freopen

Was it helpful?

Solution

Only one of the last two parameters to WriteFile can be NULL, the other must be a valid pointer.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747.aspx

In your case you probably want to use lpNumberOfBytesWritten.

DWORD written;
WriteFile(out, "num", sizeof("num"), &written, NULL);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top