是否有方法在Ubuntu Linux上使用C编程语言将彩色文本打印到文件中,可以使用printf()的颜色代码并将彩色文本打印到控制台或终端窗口,但是如何将文本打印到带颜色的文本文件?C fprintf颜色
回答
简短的答案是你不能,最长的答案是你需要写一些标记和你的文字一起提供许多格式。没有可以使用的通用标准,所以您需要选择一个。
颜色代码是特定于控制台的,并且对于打印无用,如果您只是将文本重新显示回屏幕,没有问题,但如果要打印它,则需要执行一些工作。
最简单的方法是编写HTML标签在浏览器中查看并打印。使用打印机的标记代码写入打印机也是可能的,在某些打印机上它可能很简单(或几乎不可能)。缺点是这是打印机特定的解决方案。如果你真的有雄心勃勃的PDF格式输出(它只是文本),RTF或任何其他文档格式,但这需要时间和技能。
谢谢你的回答,是的,我会采取简短的回答,在Linux上纯文本c中没有颜色,因为它的读写非常简单,Linux中的所有内容都是一个文件,所以如果我想用颜色,无论是在终端或打印到文本文件,并添加HTML标签,并指定文本的颜色。 –
我使用这个解决方案,如果你是到流和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;
}
太棒了,再次感谢。 –
不客气。对不起,在这个例子中不包括任何脸孔,但是这些也可以,例如, ''set_face(faces :: bold)<<“Bold”'给出了一张大胆的脸。 –
谢谢,我通常用C语言编写,但C++有时候有一种更简洁的方式来组织代码。 –
- 1. C++ OpenGL - 颜色
- 2. C#让所有的颜色从颜色
- 3. 用C#显示颜色的颜色?
- 4. C#相当于fprintf
- 5. 替换颜色C#
- 6. 关于fprintf的C风格
- 7. matlab fprintf处理* .c文件
- 8. C编程fprintf问题
- 9. Objective-c NavigationBar颜色变化?
- 10. 颜色定义在C#
- 11. 使用C#背景颜色
- 12. C++淡入颜色? (Arduino的)
- 13. C#相反的颜色
- 14. Visual Studio C#代码颜色
- 15. C#Trackbar自定义颜色
- 16. C#richbox改变颜色
- 17. RBG C颜色发生器
- 18. C#组合框颜色
- 19. C#循环遍历颜色?
- 20. C++复选框颜色
- 21. ANSI颜色C和ncurses的
- 22. C#aforge颜色检测
- 23. 获取颜色值C#
- 24. C++中的颜色刻度
- 25. C#标签颜色变化
- 26. C#如何增亮颜色
- 27. C++ OpenGL - 顶点颜色
- 28. objective-c文字颜色
- 29. C#自定义颜色黑色
- 30. 褪色与C循环中的颜色#
彩色输出使用的终端仿真的问题,而不是printf函数的C的问题,这是非常整洁。更好地使用termcap a/o ncurses或类似的东西 – ikrabbe
您引用的那些代码仅在终端的上下文中才有意义。如果你将文件转储到终端,你会看到相同的颜色......但是本身没有文本文件中的“彩色文本”。 – FatalError
大多数终端可以打印彩色输出,例如'printf(“\ 033 [33; 1mBold Yellow \ 033 [0m)”'将以粗体黄色打印“粗体黄色”。 –