Monday, June 6, 2022

[SOLVED] GNU/Linux replacements for Turbo C functions `clrscr` and `cprintf`

Issue

I just moved to Linux for just a month. I've used Borland Turbo C for C programming but some of these functions do not work in GNU/Linux, so looking for help.

These are some of the functions I would like to replace:
- gotoxy
- cprintf
- clrscr
- initgraph/graphics.h

I would appreciate some code examples showing how to use any replacements.


Solution

In linux, you can use the ncurses library to use the terminal as a text buffer: move the cursor around, and write text. It can also draw windows and other hi-level widgets.

For gotoxy see move and wmove from ncurses (link). For cprintf see printw. You can clear the screen simply with clear().

In ncurses you also need to refresh the screen with refresh() after printw and clear().

Example program, which uses all the mentioned functions in ncurses:

#include <curses.h>

int main(int argc, char *argv[])
{
    initscr();
    clear();
    move(15, 20);
    printw("Test program: %s", argv[0]);
    refresh();
    getch();
    endwin();
    return 0;
}

Compile in gcc with: gcc program.c -lcurses

As for graphics, you have to choose a particular library. If you need a similar experience as the low-level graphics.h, you are probably looking for directfb or svgalib. If you want to render graphics in a window, SDL will be helpful.



Answered By - MichaƂ Trybus
Answer Checked By - Pedro (WPSolving Volunteer)