久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Windows 程序在 C++ 中獲得控制臺輸出?

How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
本文介紹了如何使用 Windows 程序在 C++ 中獲得控制臺輸出?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

如果我有一個本地 C++ windows 程序(即入口點(diǎn)是 WinMain),我如何查看 std::cout 等控制臺函數(shù)的輸出?

If I have a native C++ windows program (i.e. the entry point is WinMain) how do I view output from console functions like std::cout?

推薦答案

查看 添加Win32 GUI 應(yīng)用程序的控制臺 I/O.這可能會幫助您做您想做的事.

Check out Adding Console I/O to a Win32 GUI App. This may help you do what you want.

如果您沒有或無法修改代碼,請嘗試找到 here 將控制臺輸出重定向到文件.

If you don't have, or can't modify the code, try the suggestions found here to redirect console output to a file.

這里有點(diǎn)死靈法術(shù).我第一次回答這個問題是在 9 年前,在 SO 的早期,在非鏈接答案的(好的)政策生效之前.我會重新發(fā)布原始文章中的代碼,希望能彌補(bǔ)我過去的罪過.

bit of thread necromancy here. I first answered this 9ish years ago, in the early days of SO, before the (good) policy of non-link-only answers came into effect. I'll repost the code from the original article in the hope to atone for my past sins.

guicon.cpp -- 控制臺重定向功能

#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#ifndef _USE_OLD_IOSTREAMS
using namespace std;
#endif
// maximum mumber of lines the output console should have
static const WORD MAX_CONSOLE_LINES = 500;
#ifdef _DEBUG
void RedirectIOToConsole()
{
    int hConHandle;
    long lStdHandle;
    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;
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);

    // redirect unbuffered STDOUT to the console
    lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    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 );

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

#endif
//End of File

guicon.h -- 控制臺重定向功能接口

#ifndef __GUICON_H__
#define __GUICON_H__
#ifdef _DEBUG

void RedirectIOToConsole();

#endif
#endif

// End of File

test.cpp -- 演示控制臺重定向

#include <windows.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#ifndef _USE_OLD_OSTREAMS
using namespace std;
#endif
#include "guicon.h"


#include <crtdbg.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    #ifdef _DEBUG
    RedirectIOToConsole();
    #endif
    int iVar;

    // test stdio
    fprintf(stdout, "Test output to stdout
");
    fprintf(stderr, "Test output to stderr
");
    fprintf(stdout, "Enter an integer to test stdin: ");
    scanf("%d", &iVar);
    printf("You entered %d
", iVar);

    //test iostreams
    cout << "Test output to cout" << endl;
    cerr << "Test output to cerr" << endl;
    clog << "Test output to clog" << endl;
    cout << "Enter an integer to test cin: ";
    cin >> iVar;
    cout << "You entered " << iVar << endl;
    #ifndef _USE_OLD_IOSTREAMS

    // test wide iostreams
    wcout << L"Test output to wcout" << endl;
    wcerr << L"Test output to wcerr" << endl;
    wclog << L"Test output to wclog" << endl;
    wcout << L"Enter an integer to test wcin: ";
    wcin >> iVar;
    wcout << L"You entered " << iVar << endl;
    #endif

    // test CrtDbg output
    _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
    _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR);
    _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR);
    _RPT0(_CRT_WARN, "This is testing _CRT_WARN output
");
    _RPT0(_CRT_ERROR, "This is testing _CRT_ERROR output
");
    _ASSERT( 0 && "testing _ASSERT" );
    _ASSERTE( 0 && "testing _ASSERTE" );
    Sleep(2000);
    return 0;
}

//End of File

這篇關(guān)于如何使用 Windows 程序在 C++ 中獲得控制臺輸出?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I calculate the week number given a date?(如何計(jì)算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機(jī)的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時導(dǎo)出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
The program can#39;t start because libgcc_s_dw2-1.dll is missing(程序無法啟動,因?yàn)槿鄙?libgcc_s_dw2-1.dll)
主站蜘蛛池模板: 欧美极品一区 | 日韩在线精品视频 | 九色av| 欧美精品色 | 日韩久久久久 | 伊人9999| 亚洲小视频 | 91综合网| 成人羞羞网站 | 久久精品久久久久久久 | 欧美顶级黄色大片免费 | 欧美日本在线观看 | 91av视频| 亚洲精品不卡 | 日韩欧美三区 | 成人av资源 | 三级黄色在线观看 | 伊人久久av | 91精品又粗又猛又爽 | 国产精品福利在线观看 | 免费av一区二区 | 亚洲在线一区 | 亚洲欧美日韩成人 | www.婷婷 | 久草资源在线观看 | 一级免费毛片 | 中文字幕+乱码+中文乱码91 | 91欧美激情一区二区三区成人 | 秘密爱大尺度做爰呻吟 | 精品久久免费视频 | 久久久久久综合 | 玖玖在线观看 | 婷婷中文字幕 | 日韩久久久 | 国产精品羞羞答答 | 日韩小视频在线观看 | 久久一区二区视频 | 毛片视频免费 | 国产精品二区一区二区aⅴ污介绍 | 九九热视频在线 | 国产福利视频 |