How to use the ANSI Escape code for outputting colored text on Console(如何使用 ANSI 转义码在控制台上输出彩色文本)
问题描述
我在此处阅读了有关 ANSI-C 转义码的信息.尝试在 C/C++ printf/cout 中使用它对输出到 consolde 的文本进行着色,但没有成功.
I read about ANSI-C escape codes here. Tried to use it in C/C++ printf/cout to colorize the text outputted to consolde but without sucess.
代码:
#include <iostream>
#include <cstdio>
int main()
{
int a=3, b=5;
int &ref = a;
ref = b;
//cout << "15532m" << a << b <<'
'; //here it prints m→m 5, no colored text
printf("15532m %d",a); //here to it prints same - m→m 5,
getchar();
}
如何使用这些转义码将彩色文本输出到控制台?
How to use these escape codes to output colored text to console?
我错过了什么吗?
在一些 C++ 代码中,我看到了对这个函数的调用
In some C++ code I saw a call to this function
textcolor(10);
但是它在 g++ 和 Visual Studio 中给出了编译错误.哪个编译器有这个功能?有什么细节吗?
But it gives compilation errors in g++ and in Visual Studio. Which compiler had this function available? Any details?
推荐答案
恐怕你忘记了 ESC 字符:
I'm afraid you forgot the ESC character:
#include <cstdio>
int main()
{
printf("%c[%dmHELLO!
", 0x1B, 32);
}
不幸的是,它只适用于支持 ANSI 转义序列的控制台(如使用 bash 的 linux 控制台,或使用 ansi.sys 的旧 Windows 控制台)
Unfortunately it will only work on consoles that support ANSI escape sequences (like a linux console using bash, or old Windows consoles that used ansi.sys)
这篇关于如何使用 ANSI 转义码在控制台上输出彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 ANSI 转义码在控制台上输出彩色文本
基础教程推荐
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- c++ STL设置差异 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
