如何使用ANSI转义code有关控制台输出彩色文本 [英] How to use the ANSI Escape code for outputting colored text on Console

查看:346
本文介绍了如何使用ANSI转义code有关控制台输出彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到ANSI-C转义codeS 这里 。试图在C使用/ C ++的printf / COUT上色输出到consolde但没有sucess文本。

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.

code:

#include <iostream>

 #include <cstdio>

int main()
{

    int a=3, b=5;
    int &ref = a;

    ref = b;

    //cout << "\155\32\m" << a << b <<'\n'; //here it prints m→m 5, no colored text
    printf("\155\32\m %d",a); //here to it prints same - m→m 5, 

    getchar();

}

如何使用这些转义codeS到输出彩色文本到控制台?

How to use these escape codes to output colored text to console?

我这么想吗?

编辑:在一些C ++ code,我看到这个函数的调用

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!\n", 0x1B, 32);
}

不幸的是,它只能在支持(使用bash所使用ANSI.SYS像Linux控制台,或旧的Windows控制台)ANSI转义序列的控制台上工作

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转义code有关控制台输出彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆