How to use setfillstyle() and textcolor() in C

2019-08-26 10:16发布

问题:

I want to use setfillstyle() and textcolor() in UBUNTU(terminal) .

But I found on internet that it is store in conio.h library which cannot be used in UBUNTU .

So what should I do ?

回答1:

The de facto way to do these things on unixy terminals today is to use some Curses libray, which on Ubuntu is Ncurses library developed by GNU Project.

Google for "ncurses tutorial" to get started. It is different from conio.h, so just learn it from scratch.



回答2:

That's true. setfillstyle and textcolor are functions of the old conio MS/DOS library. It can only be used nowadays in Windows consoles (unless you manage to use a real MS/DOS or FreeDOS...).

If you want to use color effect in a Linux terminal window, you could have a look at curses which normally exists in Ubuntu and allows to create portable programs.



回答3:

First of all, controlling a "screen" (or however you want to call it) is not in the scope of C. C I/O (stdio.h) only operates on input and output streams. Therefore, you either have to write platform-specific code yourself or use a library.

About conio.h, as other answers already state, this is an obsolete header. It was created for MS-DOS (AFAIK by Borland for their TurboC product) and as a result is by design strongly coupled to what a typical PC in textmode with BIOS routines provides. Although you probably find some implementations of it for other systems than MS-DOS, I'd strongly recommend not to use it.

The de facto standard nowadays for controlling a console/terminal is indeed curses, which first emerged on Unix systems. It was part of a commercial system, but free implementations for a variety of systems exist. Using curses, your code will be portable to many systems, including virtually all *nix derivates as well as Windows.

Two implementations of curses are very widespread:

  • ncurses from the GNU project, this is portable to many *nix systems and more recently to windows as well (I'd recommend it for Linux, FreeBSD etc.)

  • pdcurses which is portable as well, but with a focus on DOS and Windows (I'd recommend it for Windows)

For learning how to use curses, the NCURSES Programming HOWTO is a great resource. I personally recommend to #include <curses.h> everywhere this HOWTO suggests #include <ncurses.h>, so your code can be used with other curses implementations without modification.



回答4:

As suggested, ncurses is the usual place to start looking with Ubuntu.

According to this page

setfillstyle function sets the current fill pattern and fill color.

and goes on to list the available fill-styles:

enum fill_styles 
{ 
   EMPTY_FILL, 
   SOLID_FILL, 
   LINE_FILL, 
   LTSLASH_FILL, 
   SLASH_FILL,
   BKSLASH_FILL, 
   LTBKSLASH_FILL, 
   HATCH_FILL, 
   XHATCH_FILL, 
   INTERLEAVE_FILL,
   WIDE_DOT_FILL, 
   CLOSE_DOT_FILL, 
   USER_FILL 
};

There's no portable equivalent to the fill pattern (it's dependent upon the terminal type). Depending on what you want, <conio.h> is used for coloring full-screen or command-line applications. With the latter, you have tput (a curses utility), which can set foreground and/or background colors.

For the former, ncurses implements the X/Open background character, which can be used in full-screen applications to provide a combination of a fill-character and/or color. You could imitate conio's fill-style using the background character (though I haven't seen anyone make a table of corresponding Unicode values for those). EMPTY_FILL is easy: just a space character.

Further reading:

  • tput, reset - initialize a terminal or query terminfo database
  • bkgrnd, wbkgrnd, bkgrndset, wbkgrndset, getbkgrnd, wgetbkgrnd - curses window complex background manipulation routines
  • test/sample programs (several use the background character)