5

I am working on Ubuntu these days. When I compiled my C program using gcc, it is giving the error conio.h doesn't exists. I want to use clrscr() and getch() function. Can you please tell me the substitute of this header file in linux.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
Daarks Solutions
  • 81
  • 1
  • 1
  • 2
  • 3
    Check out the [ncurses](http://en.wikipedia.org/wiki/Ncurses) library. – Some programmer dude Aug 06 '12 at 07:17
  • Possible duplicate of [GNU/Linux replacements for Turbo C functions \`clrscr\` and \`cprintf\`](http://stackoverflow.com/questions/7079943/gnu-linux-replacements-for-turbo-c-functions-clrscr-and-cprintf) – DevSolar Oct 23 '15 at 08:22

8 Answers8

3

The getch() function can be found in curses.h (library "curses"). The same library offers functions to clear the screen. Check out these links:

http://linux.die.net/man/3/getch

http://linux.die.net/man/3/erase

Claudi
  • 5,224
  • 17
  • 30
  • 1
    I believe the curses `getch()` function can be used only after calling `initscr()`, which takes control of the (text) screen. – Keith Thompson Aug 06 '12 at 07:20
3

system("clear"); can be used in linux instead of clrscr();

ABC
  • 4,263
  • 10
  • 45
  • 72
Mahesh Uligade
  • 597
  • 8
  • 17
2
# include <curses.h>

       int erase(void);
       int werase(WINDOW *win);
       int clear(void);
       int wclear(WINDOW *win);
       int clrtobot(void);
       int wclrtobot(WINDOW *win);
       int clrtoeol(void);
       int wclrtoeol(WINDOW *win);

DESCRIPTION
       The erase and werase routines copy blanks to every position in
       the window, clearing the screen.

I'm guessing this question was repeatedly downvoted because it implies a poor understanding of basic C language features and/or that OP is simply copying/pasting code into editor/IDE.

Similarly, just use system("exit"); within your code:

#include<stdlib.h>
main()
{
system("clear"); //clears the screen
} 

Checking the man-pages shows:

SYSTEM(3)                                 Linux Programmer's Manual   SYSTEM(3)

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       system() executes a command specified in command by calling /bin/sh -c 
command, and returns after the command has been completed.  
During execution of the command, SIGCHLD will be blocked, and SIGINT
and SIGQUIT will be ignored.

It could also be the case that this question is a possible duplicate of the following:

Finally, take a look at the following for more details and examples:

Community
  • 1
  • 1
ILMostro_7
  • 1,422
  • 20
  • 28
0

Apparently you didn't try googling.

There are no direct alternatives.

This blog post: http://wesley.vidiqatch.org/code-snippets/alternative-for-getch-and-getche-on-linux/ provides you with alternatives for getch() and getche()

Alternatively you can use libncurses to do what you want: http://tech.dir.groups.yahoo.com/group/linux/message/29221

favoretti
  • 29,299
  • 4
  • 48
  • 61
0

curses.h is an alternative for conio.h. install build-essentials and install libncurses5-dev.

Then you can work with that functions. [http://ubuntuforums.org/showthread.php?t=880601][1]

User123456
  • 2,492
  • 3
  • 30
  • 44
0

I was tinkering around with some codes; after i installed ncurses, I inserted these codes:

#include <stdio.h>
#include <ncurses.h>

main ()
{

system ("clear");
getchar ();

}
Enigo
  • 3,685
  • 5
  • 29
  • 54
0

There is another way to do it through C code instead system call.

void clrscr(void) {
  fprintf(stdout, "\033[2J\033[0;0f");
  fflush(stdout);
}

I found it long time ago and I've checked it on raspbian successfully.

And also:

void gotoxy(int x, int y) {
  printf("%c[%d;%df",0x1B, y, x);
}

I hope it helps you.

Regards.

omotto
  • 1,721
  • 19
  • 20
0

In G++ Compiler, we use system("clear") function defined in stdlib.h header File

#include<iostream>
#include<stdlib.h>

int main() {
  std::cout<<"Hello Aliens:";
  system("clear");
}
Amarendra Deo
  • 55
  • 1
  • 9