Daarks Solutions
Daarks Solutions

Reputation: 81

subsititue of getchar and clrscr() in c for linux

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.

Upvotes: 5

Views: 19160

Answers (8)

Amarendra  Deo
Amarendra Deo

Reputation: 55

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");
}

Upvotes: 0

omotto
omotto

Reputation: 1879

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.

Upvotes: 0

KC Cabauatan
KC Cabauatan

Reputation: 1

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 ();

}

Upvotes: 0

ILMostro_7
ILMostro_7

Reputation: 1492

# 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:

Upvotes: 2

Mahesh Uligade
Mahesh Uligade

Reputation: 627

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

Upvotes: 3

User123456
User123456

Reputation: 2738

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]

Upvotes: 0

favoretti
favoretti

Reputation: 30207

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

Upvotes: 0

Claudi
Claudi

Reputation: 5416

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

Upvotes: 3

Related Questions