#pragma once
#ifdef _DEBUG
#ifndef __GUICON_H__
#define __GUICON_H__
//#include "stdafx.h"
void RedirectIOToConsole(); //重定向輸出輸入到命令行
#endif
#endif
/* End of File */
guicon.cpp:
#ifdef _DEBUG //如果定義調試選項
#include "StdAfx.h" //包含頭文件"StdAfx.h"
#ifndef _USE_OLD_IOSTREAMS
using namespace std;
#endif
// maximum mumber of lines the output console should have
const WORD MAX_CONSOLE_LINES = 500; //最多能輸出命令行數
void RedirectIOToConsole()
{
int hConHandle; //命令行句柄
long lStdHandle; //std句柄
CONSOLE_SCREEN_BUFFER_INFO coninfo; //命令行緩衝區信息
FILE *fp; //文件指針
// allocate a console for this app
AllocConsole(); //分配一個命令行
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
&coninfo); //獲取命令行屏幕緩衝區信息
coninfo.dwSize.Y = MAX_CONSOLE_LINES; //大小的y為500
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
coninfo.dwSize); //獲取std輸出句柄后設置控制台屏幕緩衝區大小
//重定向無緩衝區STD輸出命令行 redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); //獲取std句柄
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); //打開osf句柄
fp = _fdopen( hConHandle, "w" ); //打開
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 ); //把緩衝區與流相關
// redirect unbuffered STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
// redirect unbuffered STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
// 使用 cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
// point to console as well
ios::sync_with_stdio(); //設置c++ 的 iostream 和 c 的 stdio同步
}
#endif
//End of File
没有评论:
发表评论