1

I am outputting data to a txt file on Linux using C++. Is there a way to output a portion of a line of text in a different color?

For example, I'd like to be able to write 0.000 Watts in red.

Total Power Usage of this model: 0.000 Watts

4
  • Can you write it in HTML and then render it as HTML? If so - write <font color="red">0.000 Watts</font> Commented Nov 1, 2010 at 6:32
  • The question seems to imply "and then later viewed in a terminal/xterm window with cat(1) or less(1). You might want to clarify this as you kind of have two threads of answers: those that make that assumption and those that figure a browser or down-the-line program will be reading the file. Commented Nov 1, 2010 at 7:00
  • 1
    I was curious about a text file being able to show characters in different colors. I guess HTML or a different format is way to go. I was not talking about colors in a terminal window. Commented Nov 1, 2010 at 7:08
  • But what do you mean, "show"? A .txt file is invisible magnetic or flash memory state. It can't be in color or in black-and-white until displayed somehow. How exactly will that eventually happen, I mean, you did want color, right? Commented Nov 1, 2010 at 7:19

7 Answers 7

7

Plain text files (*.txt) don't support color in any way. You will have to use a different format, such as RTF or HTML.

Sign up to request clarification or add additional context in comments.

7 Comments

at least when redisplayed on a Bash shell. This is how Ruby on Rails log files have color text.
@動靜能量: If that's the only use of the data, then it would be "how do I output in color to a terminal" rather than "to a txt file". If that's what the OP meant to ask, sure, but that's not what the question currently says.
strange... HTML or CSS text is not red either. It is only when rendered in a web browser. So you need something to redisplay it. Why is the ANSI color code answer being so attacked when the app is the shell?
Remember that plain old text files make all kinds of assumptions about what control codes do, so there is not any obvious line that is crossed when one moves from \r, \n, \b, \t, et al, to ANSI codes. In the old days the first set was more likely to be understood by a given device than the second, but by now they are all in kind of the same class, hence their extensive use by Rails, Puppet, and various other modern software systems.
@動靜能量: See the OP's comment: "I was not talking about colors in a terminal window."
|
3

If you are willing to embed ANSI terminal escape codes in your file then, sure.

Here is an example using Ruby for pseudocode. (The #{expr} syntax just formats expr with %s or %d and interpolates it into the string.)

def colorize s, code "#{code}#{s}\e[0m" end def color s, n colorize s, "\e[#{31 + n % 7}m" end 

You could uses curses or termcap, but that would be a lot more complex and is unnecessary today, as every Linux (and Mac) terminal window is going to eat the ANSI codes with no problem.

If you then view this file or the direct output via less(1), then you will want to use less -R.


Updates: C++

#include <iostream> using namespace std; void color(const char *s, int n) { cout << "\e[" << (31 + n % 7) << "m" << s << "\e[0m"; } int main() { color("How now", 0); color(" brown cow", 1); color(" Now is the", 2); color(" time.", 3); cout << endl; return 0; } 

And C99:

#include <stdio.h> void colorize(const char *s, const char *code) { printf("%s%s\e[0m", code, s); } void color(const char *s, int n) { char t[32]; sprintf(t, "\e[%dm", 31 + n % 7); colorize(s, t); } int main(void) { color("How now", 0); color(" brown cow", 1); color(" Now is the", 2); color(" time.\n", 3); return 0; } 

4 Comments

it is also strange why @DigitalRoss's answer is the same in suggesting ANSI escape sequence, but it has 2 up votes, while the other escape sequence answer all get negative votes.
Oh, this one got downvoted too, I think, it just also got upvotes. Really, there is a trend to go ahead and use ANSI codes in syslog output text. Rails does it. Puppet does it by default, and also for console output. When you deal with gigabytes of log files, it is a really nice feature.
I don't see any downvotes here (click the post score to see). Perhaps because you qualify that "if" in the first line plus say how you have to specially view these control codes (e.g. less -R) which is not the default.
oh well... i think this is really critical here... almost like "honey, will you get a dozen of eggs? oh... check if they have a pack of 6" and the wife came back, "yes I got 12, and yes, they do have a pack of 6"
1

Best way to do this is through the curses library:

http://en.wikipedia.org/wiki/Curses_%28programming_library%29

Comments

1

If you just want to do simple ones, you can use code as in

http://deathray.us/code/color_output.html

const char COL_RESET[] = "\x1b[0m"; const char RED[] = "\x1b[31m"; cout << RED << "Red looks good" << COL_RESET << endl; 

more info on: http://en.wikipedia.org/wiki/ANSI_escape_code

Comments

1

For more information about ANSI escape codes (sometimes also known as VT100 escape codes) see http://en.wikipedia.org/wiki/ANSI_escape_code.

Comments

0

If you use something like vim to view your txt file. It's possible to add syntax file that match some particular words to a particular color. The good thing about this approach is that you don't have add any color information inside the txt file itself using html tags etc. However the color won't be portable since it's in your vim settings anyway.

Comments

0

It depends on what you're doing with your text file. If you're using cat or more (or even grep, with caveats), or otherwise outputting directly to a terminal window, you can get color by using the ANSI color codes like @DigitalRoss and several others suggest. But if you use pretty much anything else to view the file -- less, Vim, EMACS, gedit, Firefox, etc. -- there will be no colors, and instead there will be what looks like garbage in your text file.

So the answer to your question is "yes, but": Yes, there is a way to put color in your text files, but it doesn't work except when it's displayed directly, without interpretation, to a VT100-compatible terminal. So I wouldn't use control codes for a text file I wanted to give to other people, because I don't know how they'll be viewing it, and I seriously doubt they'd be using cat.

If you're intending your program to write directly to a terminal, however, go to town with the color codes. But it might be nice to add some way to turn them off, because someone else might want to redirect your program's output to a file without having to filter them out.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.