我是靠谱客的博主 复杂外套,这篇文章主要介绍C++-win32-进程打开控制台-输出临时数据,现在分享给大家,希望可以做个参考。

头文件用于包含一个全局唯一对象,输出信息到控制台。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once #include <string> #include <iostream> #define _SHOW_CONSOLE //显示控制台 void AConsoleShow(); class ConsoleStream { public: //普通类型 template<typename T> inline ConsoleStream& operator<<(const T& val) { //m_oss << val; #ifdef _SHOW_CONSOLE AConsoleShow(); std::cout << val; #endif // _SHOW_CONSOLE return *this; } //接收stream对象 如换行 inline ConsoleStream& operator<<(std::ostream& (*)(std::ostream&)) { // 接收std::endl AConsoleShow(); std::cout << std::endl; return *this; } //打印行信息 template<typename T> inline void PrintLineInfo(const T& val) { #ifdef _SHOW_CONSOLE AConsoleShow(); std::cout << val << std::endl; #endif // _SHOW_CONSOLE } ~ConsoleStream(); //std::ostringstream m_oss; }; //全局对象 extern ConsoleStream Console; /*范例 Console << "123"<< std::endl<<456<<std::endl; Console.PrintLineInfo("562897"); Console << "123" << std::endl << 456 << std::endl; */

实现文件,主要实现显示控制台功能。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "AConsole.h" #include <windows.h> //声明全局对象 ConsoleStream Console; //控制台是否显示 bool gShowConsole = false; //显示控制台 void AConsoleShow() { #ifdef _SHOW_CONSOLE /**一个进程只可以拥有一个控制台的关联, 如果调用该函数的进程已经拥有一个控制台的关联, 则AllocConsole函数失败。 */ try { HANDLE hOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);//获得控制台输出句柄 if (!hOutputHandle) { AllocConsole(); freopen("conout$", "w", stdout); } } catch (const std::exception&) { } #endif // _SHOW_CONSOLE } ConsoleStream::~ConsoleStream() { FreeConsole(); }

调用范例

复制代码
1
2
3
4
Console << "123" << std::endl << 456 << std::endl; Console.PrintLineInfo("562897"); Console << "123" << std::endl << 456 << std::endl;

最后

以上就是复杂外套最近收集整理的关于C++-win32-进程打开控制台-输出临时数据的全部内容,更多相关C++-win32-进程打开控制台-输出临时数据内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部