conio.h doesn't contain textcolor()?
Asked Answered
P

3

4

I've been looking at using colours in a DOS program I'm writing in C. I was told that conio.h has the textcolor() function, but when I use it in my code, the compiler/linker throws errors at me saying I've an undefined reference to the function.

Does conio.h actually have this function or have I been told bull?

Powerless answered 30/12, 2011 at 0:56 Comment(11)
That was a Borland function, never part of the C standard and written before prefixing non-standard functions with an underscore became the law. You'll have to break into a museum or use SetConsoleTextAttribute().Batter
Ok, thanks. Is SetConsoleTextAttribute() supported by most 16-bit C compilers for DOS? :)Powerless
It's supported by none of them. Head for the museum or excavate Ralph's interrupt list.Batter
But you said to use SetConsoleTextAttribute?Powerless
Yes, my mistake, I haven't written any 16-bit code for the past 17 years. I've also haven't played any LPs for that long, forgot what OJ Simpson did and the silly putty turned into rock. You'll have to forgive an old man getting feeble.Batter
Haha ok, I will give that function a go anywayPowerless
SetConsoleTextAttribute is a Win32 console function. For DOS, DJGPP's library has a textcolor(), but DJGPP needs a 386. For 16-bit, Watcom has some functions that are similar.Montpellier
I'm using Watcom to compile, I will have to find out those functions...Powerless
I'm looking at Section 2.3.5 in the OpenWatcom manualMontpellier
this will light my day... haven't heard of Ralph's INT list for ages. Sir you'll make my day a better one (www-2.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html)Marionmarionette
never heard of Ralf's init list (I suppose I am a little newer to this world than you guys) so I checked it, download and extracted the archives. OH MY GOD :)) I feel like this guy said: Hey, internet isn't yet a never ending repository of knowledge, references, examples, how to's and stack overflows (wink wink), so I'll just create it... by my self... in one place... ok... sounds duable, let's do it. And we kids today complain with all those abundantly available at our fingers.Treasatreason
C
2

No, the conio.h library does not have the textcolor function defined. One of the ways that that function could be defined is the following (include the windows.h library):

void textcolor (int color)
{
    static int __BACKGROUND;

    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;


    GetConsoleScreenBufferInfo(h, &csbiInfo);

    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
                             color + (__BACKGROUND << 4));
}
Clunk answered 25/6, 2018 at 0:19 Comment(0)
P
0

It's a Turbo C/C++ Compiler function. It is in the conio.h header, but only if you're using a Turbo C/C++ Compiler.

See page 384 of the Turbo C/C++ Compiler 2.0 docs, which says:

Function Selects new character color in text mode.

Syntax

#include <conio.h>
void textcolor(int newcolor); 

Prototype in conio.h

Remarks: textcolor selects the foreground character color. The foreground color of all characters subsequently written by the console output functions will be the color given by newcolor. You can give the color using a symbolic constant defined in conio.h. If you use these constants, you must include conio.h.

This function does not affect any characters currently on the screen, but only those displayed using direct console output (such as cprintf) after textcolor has been called.

[...]

See also: Colorizing text in Turbo C++.

Pinniped answered 16/4, 2023 at 9:44 Comment(0)
M
-1

Check the textcolor library library, it may do just what you need.

An example, showing how to use it:

#include<stdio.h>
#include<conio.h>
main()
{
   textcolor(RED);
   cprintf("C programming");

   getch();
   return 0;
}
Manchester answered 28/5, 2015 at 10:41 Comment(1)
"check", there is the code you wrote, but this code does not work, because textcolor is not included in conio.hVinna

© 2022 - 2024 — McMap. All rights reserved.