Solved

printf - can I print in color?

Posted on 2004-08-29
8
1,695 Views
Last Modified: 2008-01-16
Hi,

When I use "printf", the printed text is silver.
Can I print in other colors aswell? (even basic 16 colors are enough).

Thanks!
0
Comment
Question by:slavikn
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926224
Not with standard C function, but may be possible.
That will depend of the compiler and library you are using.
Please specify compiler/environment.

0
 
LVL 1

Author Comment

by:slavikn
ID: 11926308
UNIX. g++. (only ANSI C is allowed)
By the way, sorry that I don't give more points - I just don't have any :-(
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 25 total points
ID: 11926323
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ankuratvb
ID: 11926333
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926364
If you are working in Linux with terminal emulation you can use ANSI escape sequences but you have to ensure you terminal type is ANSI or VT100, will not work with any terminal model emulated.
Here is a complete guide: http://www.graphcomp.com/info/specs/ansi_col.html
0
 
LVL 1

Author Comment

by:slavikn
ID: 11927099
Thanks, ankuratvb.

To jaime_olivares, I am working in Unix, rather than Linux with terminal emulation.
0
 

Expert Comment

by:danielvallas
ID: 12131458
Also, check out the TERMCAP (You are probably using TERMINFO these days) entries for the terminals in UNIX - you will find many control codes in those files for your terminals... the files are used to make a standard control code interface for the different terminals that are connected to your UNIX box - mapping defined constants to terminal escape sequences.  Your shell program you are using to log in and any program that has to communticate with the terminal uses them.  You use "curses" functions to get the control codes from the aprropriate terminal file and use them to draw boxes and move the cursor around the screen - if it is a color terminal you are using, it should have control codes defined to change colors in those files.  I remember a few years back, I used curses functions to write a recursive menu system for an MIS project.

The TERMCAP or TERMINFO are stored in the /etc directory.  The difference between TERMCAP and TERMINFO is that TERMCAP is the older version and is just a text files - TERMINFO is newer, and if I remember correctly, it is actually compiled somehow - it has been a couple of years since I have had to use it.  Check out the man pages on TERMINFO and see if it tells you how to view the files... there should be an entry defined for your terminal - just go to the /etc directory and work from there.

If you have any questions, send me an email.
0
 

Expert Comment

by:danielvallas
ID: 12131539
Oh, one other thing...

Provided you are using a color terminal, or a PC with your TERMCAP or TERMINFO shell variable set to a color terminal type... that is how you specify your terminal type to the shell in UNIX - just set that variable appropriately, check out the TERMCAP or TERMINFO files under the /etc directory and find out the escape sequences for your PC or terminal that will change the color as desired ... and just print the escape sequence to change the color out first with printf and then print your text, then you can change the color back the same way.

printf( "%sThis is a test%s\n", "\(whatever)\...", "\(whatever)\..." ); -  get it?

The only problem with this approach is that if someone trys to run your program under a different terminal type, he would probably just see garbage as the escape sequences are printed, becuase they are different with different terminals.  To do it right, you should really query the TERMINFO file using the curses functions and get the correct escape sequences by just giving the control code to the curses function - it will then look at the TERMINFO shell variable and look in the right TERMINFO terminal file to get the right escape sequence - you would then use that escape sequence in your printf - (you would have to call the curses function twice though, once to get the espcape code to change the color, and once to get the one to change it back).

And if I was not to clear, the /etc/TERMCAP and the /etc/TERMINFO files are actually directories - which have files that are named after the terminals in them.  You  probably are using TERMINFO - if you can not read the files with the vi editor, make sure you check out the man pages as to how to uncompile them so that you can view them - I can not remember the command (it has been a few years since I had to mess with it) - but do not change the files at all - unless you know what your doing and know that you are changing them correctly - you would only change escape sequence codes in the TERMCAP or TERMINFO files if they were defined wrong or you needed to add a new terminal type that was not defined previously.

I hope this helps you - send me an email if you have further problems.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question