c++ - Setting stdout/stderr text color in Windows -


i tried using system("color 24"); didn't change color in prompt. after more googling saw setconsoletextattribute , wrote below code.

this results in both stdout , stderr both getting colored red instead of stdout being green , stderr being red.

how solve this? prompt red don't care since know how fix it.

should work in windows 7. @ moment i'm building prompt (using vs 2010 cl) , running in regular cmd prompt

#include <windows.h> #include <stdio.h> int main(int argc, char **argv) {     int i;     unsigned long totaltime=0;       handle hconsoleout; //handle console     hconsoleout = getstdhandle(std_output_handle);     setconsoletextattribute(hconsoleout, foreground_green);      handle hconsoleerr;     hconsoleerr = getstdhandle(std_error_handle);     setconsoletextattribute(hconsoleerr, foreground_red);      fprintf(stdout, "%s\n", "out");     fprintf(stderr, "%s\n", "err");     return 0; } 

according msdn getstdhandle() documentation, function return handles same active console screen buffer. setting attributes using these handles change same buffer. because of have specify color right before right output device:

/* ... */  handle hconsoleout; //handle console handle hconsoleerr; hconsoleerr = getstdhandle(std_error_handle); hconsoleout = getstdhandle(std_output_handle); setconsoletextattribute(hconsoleout, foreground_green); fprintf(stdout, "%s\n", "out");  setconsoletextattribute(hconsoleerr, foreground_red); fprintf(stderr, "%s\n", "err"); return 0; 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -