PDA

View Full Version : function to clear the screen in C language



princeofindia
April 29th, 2010, 01:20 AM
This thread is for the one who knows C Programming under windows and Linux well.can anyone please tell me the function to clear the console in linux using C language.It is done under windows by using clrscr() by including <conio.h>.Also,please tell me the header file to use and the respective function.

DBQ
April 29th, 2010, 02:19 AM
Assuming you are using linux:

system("clear");

princeofindia
April 29th, 2010, 02:34 AM
But which header file to use this function?

DBQ
April 29th, 2010, 02:37 AM
#include <stdio.h>
#include <stdlib.h>

StephenF
April 29th, 2010, 11:36 AM
When it comes to taking complete control of the console you'll want to use the ncurses library.

Keeping things simple:
http://ascii-table.com/ansi-escape-sequences.php

Escape sequence example:


printf("\x1b[s\x1b[2J\x1b[10;25HTaking control of your console.\x1b[u");

slavik
April 29th, 2010, 12:32 PM
1. use ncurses
2. escape chars are evil
3. system() is evil

nvteighen
April 29th, 2010, 02:47 PM
1. use ncurses
2. escape chars are evil
3. system() is evil

+1

ncurses is the standard way to go not only on GNU/Linux but also in all the rest of UNIX and Unix-like OSs. It's a weird library in some extents, but is much nicer than all other solutions.

Also, system("clear") won't work very well when running under a virtual terminal (xterm, gnome-terminal, etc.) because scrolling the text up will still show the previous contents because "clear" actually just inserts as much whitespace necessary to place the prompt back again to the first line... at console, using Shift+PgUp will also have the same effect.