使得printf的一些文字会出现在绿色和红色 [英] Making some text in printf appear in green and red

查看:168
本文介绍了使得printf的一些文字会出现在绿色和红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

linux下的gcc 4.4.1

linux gcc 4.4.1

我有以下声明fprintf中,我想有确定为绿色,失败为红色。这可能吗?

I have the following fprintf statement and I would like to have the OK as green and the FAILED as red. Is this possible?

if(devh == -1)
{
    fprintf(stderr, "Device [ FAILED ]\n");
}
else
{
    fprintf(stderr, "Device [ OK ]\n");
}

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

您应该使用一些库,如ncurses的处理终端。

You should probably use some library such as ncurses to handle terminal.

另外,在Linux下,你可以使用一些控制台转义序列,如:

Alternatively, under Linux you could use some console escape sequences such as:

printf ("\033[32;1m OK \033[0m\n");

(在这种情况下,32看台为绿色),但它既不是便携式也不优雅

(in this case 32 stands for green), but it is neither portable nor elegant.

这篇关于使得printf的一些文字会出现在绿色和红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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