C带颜色的fprintf

C带颜色的fprintf,c,linux,file,ubuntu,C,Linux,File,Ubuntu,有没有一种方法可以在Ubuntu Linux上使用C编程语言将彩色文本打印到文件中,您可以将颜色代码与printf()一起使用,并将彩色文本打印到控制台或终端窗口,但如何将文本打印到彩色文本文件中?简单的回答是您不能,答案很长,你需要在你的文本中用多种可用的格式写一些标记。没有可以使用的通用标准,所以您需要选择一个 颜色代码是控制台特有的,对于打印不有用,如果您只是将文本重新显示回屏幕,没有问题,但如果您想打印,则需要进行一些工作 最简单的方法可能是编写HTML标记以便在浏览器中查看并从那里打印

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

简单的回答是您不能,答案很长,你需要在你的文本中用多种可用的格式写一些标记。没有可以使用的通用标准,所以您需要选择一个

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


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

简短的回答是你不能,冗长的回答是你需要在文本中以多种可用格式写一些标记。没有可以使用的通用标准,所以您需要选择一个

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


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

我使用的是这个解决方案,如果你喜欢streams和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;
}
#包括
#包括
#包括
命名空间SP{
名称空间颜色{
枚举颜色{
无=0x00,
黑色=0x01,
红色=0x02,
绿色=0x03,
黄色=0x04,
蓝色=0x05,
品红色=0x06,
青色=0x07,
白色=0x08
};
}
命名空间面{
枚举面{
正常=0x00,
粗体=0x01,
暗=0x02,
uline=0x04,
反转=0x07,
不可见=0x08,
倾斜=0x09
};
}
/**
*为std::iostream的颜色代码生成字符串
*
*@param前景
*@param背景
*
*@返回
*
*用法:
*
*使用名称空间SP;

*std::cout我正在使用这个解决方案,如果您喜欢streams和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;
}
#包括
#包括
#包括
命名空间SP{
名称空间颜色{
枚举颜色{
无=0x00,
黑色=0x01,
红色=0x02,
绿色=0x03,
黄色=0x04,
蓝色=0x05,
品红色=0x06,
青色=0x07,
白色=0x08
};
}
命名空间面{
枚举面{
正常=0x00,
粗体=0x01,
暗=0x02,
uline=0x04,
反转=0x07,
不可见=0x08,
倾斜=0x09
};
}
/**
*为std::iostream的颜色代码生成字符串
*
*@param前景
*@param背景
*
*@返回
*
*用法:
*
*使用名称空间SP;


*std::cout彩色输出是所使用的终端模拟的问题,而不是printf的c问题。最好使用termcap a/o ncurses或类似的东西。您引用的代码仅在终端上下文中有意义。如果您将文件转储到终端,您将看到相同的颜色…但没有“彩色文本”这类东西大多数终端都可以打印彩色输出,例如,“printf”(\033[33;1mBold Yellow\033[0m)”将打印“Bold Yellow”粗体黄色。@Ikrabe谢谢您的支持comment@JensMunk感谢您的评论。彩色输出是所使用的终端仿真问题,而不是打印F的c问题。最好使用termcap a/o ncurses或类似的东西。您提到的代码仅在终端上下文中有意义。如果您将文件转储到终端,您将看到e相同的颜色…但文本文件本身没有“彩色文本”这类内容。大多数终端可以打印彩色输出,例如,`printf(“\033[33;1mBold黄色\033[0m)”`将打印“粗体黄色”粗体黄色。@Ikrabe谢谢您的支持comment@JensMunk谢谢你的评论,非常感谢,欢迎你。很抱歉没有在这个例子中包含任何面部,但是这些工作也一样,例如,代码>谢谢,我通常用C写,但是C++有时有更干净的组织代码的方式。我可以用C,B版本来更新它。但用C语言应该很清楚。让函数在字符缓冲区上运行,并附加和预加所需的内容。@BelalMedhat如果你用C语言编写解决方案并上传答案,我将删除我的,并向你投票。这太棒了,再次感谢你。不客气。很抱歉没有在示例中包括任何面,但这些也可以工作,例如<代码>谢谢,我通常用C编写,但是C++有时有一种更干净的组织代码的方法。我可以用C版本来更新它,但是C应该很清楚。C使函数在char缓冲器上运行,并附加和预置所需的东西。@ BelalMedhat。如果你在C中写了一个解决方案并上传一个答案,我会删除我的UPO和UVO。谢谢你的回答,是的,我会选择简短的答案,linux上的纯文本c中没有颜色,因为它读写起来非常简单,linux中的一切都是一个文件,所以如果我想用颜色打印文本,无论是在终端还是打印到文本文件中,并添加HTML标记和为文本指定颜色。谢谢你的回答,是的,我会的以th为例