久久久久久久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)
主站蜘蛛池模板: 久久com | 成人在线免费观看 | 黑人一级片视频 | 黄网站涩免费蜜桃网站 | 久久国产一区二区三区 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | www国产成人免费观看视频,深夜成人网 | 欧美日韩国产高清视频 | 99精品久久久国产一区二区三 | 一级电影免费看 | 91av入口| 成年无码av片在线 | 中文字幕成人 | 天天干天天爽 | 天天天堂| av中文字幕在线播放 | 日本三级做a全过程在线观看 | 99这里只有精品视频 | 国产资源网 | 亚洲视频在线看 | 国产一级电影在线观看 | 天天操操操操操 | 91影院在线观看 | 国产一区二区三区在线 | 欧美日韩精品久久久免费观看 | 久久久久久久久久久成人 | 久久草在线视频 | 国产精品日韩在线 | 久久久99国产精品免费 | 久久亚洲一区 | 亚洲欧洲中文日韩 | 成人免费视频网站在线观看 | 国产情侣激情 | 欧美成年人| 久久精品国产亚洲 | 天天操,夜夜爽 | 中文字幕亚洲精品 | 亚洲一区二区久久 | 91影院| 国产激情在线播放 | 亚洲91 |