Setting console output handle in C program

 

int fd;

FILE *fp;

HANDLE hCon;

AllocConsole();

hCon = GetStdHandle(STD_OUTPUT_HANDLE);

fd = _open_osfhandle(reinterpret_cast<long>(hCon), 0);

fp = _fdopen(fd, "w");

*stdout = *fp;

setvbuf(stdout, NULL, _IONBF, 0);