Ubuntu用C/C++在终端打印不同颜色的字体
参考:
1、https://blog.****.net/buckphil/article/details/72818248
2、http://blog.sina.com.cn/s/blog_628ba3e00101jll1.html
示例:
终端运行:
$ touch test.cpp
$ gedit test.cpp
复制粘贴以下代码:
#include <iostream>
using namespace std;
int main()
{
int i=0;
cout<<endl;
cout<<"\e[1;31;40m这是第"<<++i<<"次打印输出\e[0m"<<endl<<endl;
cout<<"\e[2;32;41m这是第"<<++i<<"次打印输出\e[0m"<<endl<<endl;
cout<<"\e[3;33;42m这是第"<<++i<<"次打印输出\e[0m"<<endl<<endl;
cout<<"\e[4;34;43m这是第"<<++i<<"次打印输出\e[0m"<<endl<<endl;
cout<<"\e[9m\e[37m\e[44m这是第"<<++i<<"次打印输出\033[0m"<<endl<<endl;
//"\e[9m\e[37m\e[44m"等效于"\e[9;37;44m","\033[0m"等效于"\e[0m"
return 0;
}
编译运行:
$ g++ test.cpp -o test
$ ./test
效果: