返回> 网站首页 

[转载]在控制台输出调试信息

yoours2012-02-18 12:37:15 阅读 1289

简介一边听听音乐,一边写写文章。

#include <Fcntl.h>
#include <io.h>
using namespace std;
void CMyTestDlg::OnBnClickedButton1()
{
//void CGlobalFunc::RedirectIOToConsole()
int hConHandle;
HANDLE lStdHandle;

CONSOLE_SCREEN_BUFFER_INFO coninfo;

FILE *fp;
// allocate a console for this app
bool br = AllocConsole() == TRUE;
if(!br)
return;

// set the screen buffer to be big enough to let us scroll text
br = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo) == TRUE;
assert( br );
coninfo.dwSize.Y = 5000;
br = SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize) == TRUE;
assert( br );
// redirect unbuffered STDOUT to the console

lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
assert( lStdHandle != INVALID_HANDLE_VALUE );
assert( lStdHandle != NULL );

hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
assert( hConHandle != -1 );

fp = _fdopen( hConHandle, "w" );
assert(fp);

*stdout = *fp;
int ir = setvbuf( stdout, NULL, _IONBF, 0 );
assert(  ir ==0 );

// redirect unbuffered STDIN to the console
lStdHandle = GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);

fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
ir = setvbuf( stdin, NULL, _IONBF, 0 );
assert(  ir ==0 );

// redirect unbuffered STDERR to the console
lStdHandle = GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );

*stderr = *fp;
ir = setvbuf( stderr, NULL, _IONBF, 0 );
assert(  ir ==0 );

// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog 
// point to console as well
ios::sync_with_stdio( true );
}

微信小程序扫码登陆

文章评论

1289人参与,0条评论