2016-03-30 50 views
0

是否有方法在Ubuntu Linux上使用C编程语言将彩色文本打印到文件中,可以使用printf()的颜色代码并将彩色文本打印到控制台或终端窗口,但是如何将文本打印到带颜色的文本文件?C fprintf颜色

+0

彩色输出使用的终端仿真的问题,而不是printf函数的C的问题,这是非常整洁。更好地使用termcap a/o ncurses或类似的东西 – ikrabbe

+3

您引用的那些代码仅在终端的上下文中才有意义。如果你将文件转储到终端,你会看到相同的颜色......但是本身没有文本文件中的“彩色文本”。 – FatalError

+0

大多数终端可以打印彩色输出,例如'printf(“\ 033 [33; 1mBold Yellow \ 033 [0m)”'将以粗体黄色打印“粗体黄色”。 –

回答

0

简短的答案是你不能,最长的答案是你需要写一些标记和你的文字一起提供许多格式。没有可以使用的通用标准,所以您需要选择一个。

颜色代码是特定于控制台的,并且对于打印无用,如果您只是将文本重新显示回屏幕,没有问题,但如果要打印它,则需要执行一些工作。

最简单的方法是编写HTML标签在浏览器中查看并打印。使用打印机的标记代码写入打印机也是可能的,在某些打印机上它可能很简单(或几乎不可能)。缺点是这是打印机特定的解决方案。如果你真的有雄心勃勃的PDF格式输出(它只是文本),RTF或任何其他文档格式,但这需要时间和技能。

+0

谢谢你的回答,是的,我会采取简短的回答,在Linux上纯文本c中没有颜色,因为它的读写非常简单,Linux中的所有内容都是一个文件,所以如果我想用颜色,无论是在终端或打印到文本文件,并添加HTML标签,并指定文本的颜色。 –

0

我使用这个解决方案,如果你是到流和C++

#include <cstdlib> 
#include <iostream> 
#include <sstream> 

namespace sps { 
    namespace colors { 
    enum color { 
     none = 0x00, 
     black = 0x01, 
     red  = 0x02, 
     green = 0x03, 
     yellow = 0x04, 
     blue = 0x05, 
     magenta = 0x06, 
     cyan = 0x07, 
     white = 0x08 
    }; 
    } 
    namespace faces { 
    enum face { 
     normal = 0x00, 
     bold  = 0x01, 
     dark  = 0x02, 
     uline  = 0x04, 
     invert = 0x07, 
     invisible = 0x08, 
     cline  = 0x09 
    }; 
    } 

    /** 
    * Generate string for color codes for std::iostream's 
    * 
    * @param foreground 
    * @param background 
    * 
    * @return 
    * 
    * Usage: 
    * 
    * using namespace sps; 
    * std::cout << "These words should be colored [ " 
    *   << set_color(colors::red) << "red " 
    *   << set_color(colors::green) << "green " 
    *   << set_color(colors::blue) << "blue" 
    *   << set_color() << " ]" << std::endl; 
    * 
    */ 
    static inline std::string set_color(sps::colors::color foreground = sps::colors::none, 
             sps::colors::color background = sps::colors::none) { 
    std::stringstream s; 
    s << "\033["; 
    if (!foreground && ! background){ 
     s << "0"; // reset colors if no params 
    } 
    if (foreground) { 
     s << 29 + foreground; 
     if (background) s << ";"; 
    } 
    if (background) { 
     s << 39 + background; 
    } 
    s << "m"; 
    return s.str(); 
    } 

    /** 
    * 
    * 
    * @param face 
    * 
    * @return 
    */ 
    static inline std::string set_face(sps::faces::face face = sps::faces::normal) { 
    std::stringstream s; 
    s << "\033["; 
    if (!face) { 
     s << "0"; // reset face 
    } 
    if (face) { 
     s << face; 
    } 
    s << "m"; 
    return s.str(); 
    } 
} 

int main(int agrc, char* argv[]) 
{ 
    using namespace sps; 
    std::cout << "These words should be colored [ " 
     << set_color(colors::red) << "red " 
     << set_color(colors::green) << "green " 
     << set_color(colors::blue) << "blue " 
     << set_color(colors::cyan) << "cyan " 
     << set_color(colors::magenta) << "magenta " 
     << set_color(colors::yellow) << "yellow" 
     << set_color() << " ]" << std::endl; 
    return EXIT_SUCCESS; 
} 
+1

太棒了,再次感谢。 –

+0

不客气。对不起,在这个例子中不包括任何脸孔,但是这些也可以,例如, ''set_face(faces :: bold)<<“Bold”'给出了一张大胆的脸。 –

+0

谢谢,我通常用C语言编写,但C++有时候有一种更简洁的方式来组织代码。 –