Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1719
  • Last Modified:

printf - can I print in color?

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
slavikn
Asked:
slavikn
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
slaviknAuthor Commented:
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Jaime OlivaresSoftware ArchitectCommented:
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
 
slaviknAuthor Commented:
Thanks, ankuratvb.

To jaime_olivares, I am working in Unix, rather than Linux with terminal emulation.
0
 
danielvallasCommented:
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
 
danielvallasCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now