MFC中 调用控制台

来源:互联网 发布:淘宝交易风险保障 15天 编辑:程序博客网 时间:2024/05/16 18:55

法一:

     在OnInitDialog中加入以下代码:

          AllocConsole();  

freopen("CONOUT$","w+t",stdout);  
freopen("CONIN$","r+t",stdin);  
//cout << "Input:" <<endl;
printf("str   =   %s\n",   "debug");
printf("sfffff= %c%s",'d',"cesshi");
//getchar();
//system("pause") ;//防止控制台一闪而过的两种方法

//fclose(stdout);
//fclose(stdin);

//FreeConsole();

法二:

#include <io.h>
#include <fcntl.h>
#include <stdio.h>

void InitConsoleWindow()
{
int nCrt = 0;
FILE* fp;
AllocConsole();
nCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
fp = _fdopen(nCrt, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
}

InitConsoleWindow();

printf("str   =   %s\n",   "debug");



0 0