1.在MainFrame.h中添加头文件:
#include <io.h>
#include <fcntl.h>
2.在MainFrame.h中声明函数:
public:
bool OpenConsole();
3.在MainFrame.cpp中添加函数定义:
bool CMainFrame::OpenConsole()
{
// 保证函数只执行一次
static bool Runed = false;
if (Runed)
return false;
Runed = true;
bool result;
HANDLE hStd;
FILE* fp;
long hFile;
result = AllocConsole();
hStd = GetStdHandle(STD_INPUT_HANDLE);
hFile = (long)_open_osfhandle((intptr_t)hStd, _O_TEXT);
fp = _fdopen(hFile, "r");
(*stdin) = (*fp);
hStd = GetStdHandle(STD_OUTPUT_HANDLE);
hFile = (long)_open_osfhandle((intptr_t)hStd, _O_TEXT);
fp = _fdopen(hFile, "w"); // 注意:必须是可写模式
(*stdout) = (*fp);
hStd = GetStdHandle(STD_ERROR_HANDLE);
hFile = (long)_open_osfhandle((intptr_t)hStd, _O_TEXT);
fp = _fdopen(hFile, "w"); // 注意:必须是可写模式
(*stderr) = (*fp);
cout << "打开控制台成功" << endl;
return result;
}
5.在MainFrame的构造函数中添加
OpenConsole();
6.在要cout的类的头文件中加入如下代码:
#include<iostream>
using namespace std;
完。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)