Alright, I have an assignment in which I have to use ANSI Escape Sequences to colorize output on the console. So "\033[0;31m" colorizes text after it to be red for example.

When I run the following:

cout << "\033[0;31mhello \033[0;37mworld";

It works as it is supposed to, with "hello" printing in red, and then going back to grey for "world"

Although, when I return this exact same string from a function and print it, it doesn't work, it just prints out the escape sequence. I am pulling my hair out here. I don't know if anyone would know anything about this. Any insight would be appreciated.

Recommended Answers

All 4 Replies

It doesn't work on modern MS-Windows operating system. Those escept codes are intended for Win95 and MS-DOS Version 6.X and earlier which had ansi.sys device driver installed. ansi.sys has not been in use since probably Windows NT 4.0. See this Microsoft article.

It doesn't work on modern MS-Windows operating system. Those escept codes are intended for Win95 and MS-DOS Version 6.X and earlier which had ansi.sys device driver installed. ansi.sys has not been in use since probably Windows NT 4.0. See this Microsoft article.

I got it working... it was just because the escape character '/033' has to be one character instead of 4. Actually, ansi.sys and these codes still work with the command prompt on newer versions of windows: http://en.wikipedia.org/wiki/Ansi.sys (wikipedia, but none the less). I didn't have to do anything to make them work on my version of XP.

I am now using Windows 7 and ansi.sys isn't on my computer. Please repost the line that works and I'll test it.

Yeah, it probably isn't included in Windows 7. The wikipedia article only says it is included up to Vista. The line that I posted above should work if you do have it. What I was having trouble with before is getting the codes to work in a string that was return from a function. But that was just because the escape character was being processed as 4 characters in that case. Thanks for your response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.