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

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

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

問題描述

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

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 應用程序的控制臺 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.

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

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

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

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

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
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(程序無法啟動,因為缺少 libgcc_s_dw2-1.dll)
主站蜘蛛池模板: 国产精品一区二区不卡 | 中文成人在线 | 中文字幕成人 | 97色在线视频 | 欧美一区二区小视频 | 国产亚洲精品综合一区 | 色综合视频 | av影片在线 | 亚洲欧美一区二区三区国产精品 | 国产精品久久久久久一区二区三区 | 国产区在线视频 | 日韩精品一区二区三区中文在线 | av色站 | 久草资源 | 午夜精品久久久久久久星辰影院 | 五月激情综合 | 久久久久久成人 | 日韩三级一区 | 国产精品色婷婷久久58 | 色视频一区二区 | 久久久国产精品 | 久久99深爱久久99精品 | 国产韩国精品一区二区三区 | 国产视频一区在线 | 美女在线观看国产 | 久久亚洲国产精品日日av夜夜 | 国产一二区视频 | 欧美激情精品久久久久 | 极品的亚洲 | 国产精品久久久久久久7电影 | 久久亚洲一区二区 | 91黄色免费看 | 亚洲毛片在线 | 亚洲午夜视频 | 一级片免费视频 | 99福利视频导航 | 亚洲成人网在线观看 | 国产精品一级在线观看 | 97久久精品午夜一区二区 | 亚洲第一区国产精品 | 成人二区|