When I ran this command on linux $ gcc, it gave me an output like this:
gcc: fatal error: no input files compilation terminated. Where, gcc was in bold white color and fatal error: was in bold red color.
And then I ran this command $ gcc &> err.txt and the content of err.txt was:
gcc: fatal error: no input files compilation terminated. With no ANSI color codes, just the plain text.
But with my program written in C++, the text file's content was:
[1;91merr:[0m no arguments were passed, try using '--help' Where err: was in bold red color with showed up in terminal but the file too contains those characters.
My sample code is:
if (argc == 1) { std::fprintf(stderr, "\033[1;91merr:\033[0m no arguments were passed, try using '--help'\n"); return EXIT_FAILURE; } My Question:
- How does
gccand other linux programs prints colored text on terminal without using ANSI color codes? - Are there any other methods to print colored output on terminal except ANSI color codes?
- Are there any cross-platform libraries todo so?
isattyto check if the standard output is connected to a terminal or something else (redirected, pipe, etc).SetConsoleMode(... ENABLE_VIRTUAL_TERMINAL_PROCESSING). See stackoverflow.com/a/51777740/567292isattyon Windows isGetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE))or similar. See github.com/gcc-mirror/gcc/blob/… for how gcc does it.