Ubuntu用C/C++在终端打印不同颜色的字体

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lixujie666/article/details/83544869

参考:

1、https://blog.csdn.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

效果:

猜你喜欢

转载自blog.csdn.net/lixujie666/article/details/83544869
今日推荐