C语言windows窗口程序创建控制台窗口输出printf
方式一
void CreateAndAttachConsole() {
if (::AllocConsole()) {
FILE *unused;
if (freopen_s(&unused, "CONOUT$", "w", stdout)) {
_dup2(_fileno(stdout), 1);
}
if (freopen_s(&unused, "CONOUT$", "w", stderr)) {
_dup2(_fileno(stdout), 2);
}
//std::ios::sync_with_stdio();
//FlutterDesktopResyncOutputStreams();
}
//设置控制台编码为UTF-8
system("CHCP 65001\n");
}
方式二
AllocConsole();
freopen("CONOUT$", "w", stdout);
//设置控制台编码为UTF-8
system("CHCP 65001\n");