Skip to contentSkip to conversation

clear the screen

DISCUSSION kaushal 8 participants 9 messages Feb 21, 2005 – Feb 28, 2005

lore
Share & tools
lore

kaushal

author
#0 lore raw

hello all,
Sorry for a very basic question.I would like to know how to clear the
screen from within a c program without using system("clear") or any of the ncurses functions.Can we do this using printf?My program is in raw mode so is there any feature available by which I can use the termios structure for clearing the screen if it is not possible with printf.Long ago when I used work in the dos platform I used conio.h and clrscr() rourine.I guess I have to write that clrscr() routine's code itself.Im working on a linux machine now.

Thanks in advance.
-kaushal.

#1 lore raw
Quoted 10 lines · kaushal

hello all,
Sorry for a very basic question.I would like to know how to clear the
screen from within a c program without using system("clear") or any of the ncurses functions.Can we do this using printf?My program is in raw mode so is there any feature available by which I can use the termios structure for clearing the screen if it is not possible with printf.Long ago when I used work in the dos platform I used conio.h and clrscr() rourine.I guess I have to write that clrscr() routine's code itself.Im working on a linux machine now.

Quoted 2 lines

Thanks in advance.
-kaushal.

Don't know if this is of any help to you:

#include <stdio.h>

int main(void) {

printf("\e[0m\e[2J\e[1;1H");

return 0;
}

When the compiler is called with some warning flags it might complain about a non-standard escape sequence, but it compiles fine and it works.

Goodluck & Regards,

J.

--
http://www.rdrs.net

#2 lore raw
Quoted 1 line
Quoted 1 line

printf("\e[0m\e[2J\e[1;1H");

How does this printf work to clear the screen?

#3 lore raw
Quoted 3 lines · On Mon, 21 Feb 2005 15:26:51 +0530 "Vinodkumar Nair"…
Quoted 1 line
Quoted 1 line

printf("\e[0m\e[2J\e[1;1H");

How does this printf work to clear the screen?

It's ascii control character. 2J means clear the screen and 1;1H set the cursor position.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Best regards,
yyw@smth
#4 lore raw

system("clear");

Quoted 16 lines · YYW
Quoted 3 lines · On Mon, 21 Feb 2005 15:26:51 +0530 "Vinodkumar Nair"…
Quoted 1 line
Quoted 1 line

printf("\e[0m\e[2J\e[1;1H");

How does this printf work to clear the screen?

It's ascii control character. 2J means clear the screen and 1;1H set the cursor position.

Quoted 6 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

=)
u misunderstood the original post/question by kaushal

Quoted 39 lines · Darren Sessions

system("clear");

Quoted 5 lines · YYW

On Mon, 21 Feb 2005 15:26:51 +0530 "Vinodkumar Nair" <[email protected]> wrote:

Quoted 18 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

printf("\e[0m\e[2J\e[1;1H");

How does this printf work to clear the screen?

It's ascii control character. 2J means clear the screen and 1;1H set the cursor position.

Quoted 7 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to kaushal
Quoted 8 lines · kaushal

Sorry for a very basic question.I would like to know how to clear the
screen from within a c program without using system("clear") or any of the ncurses functions.Can we do this using printf?My program is in raw mode so is there any feature available by which I can use the termios structure for clearing the screen if it is not possible with printf.Long ago when I used work in the dos platform I used conio.h and clrscr() rourine.I guess I have to write that clrscr() routine's code itself.Im working on a linux machine now.

Unix systems don't have a "screen", they have terminals. Historically, these would be physical terminals (e.g. vt220s) connected via serial ports. The Linux kernel uses the keyboard and the graphics card's text mode to emulate a number of terminals, as well as supporting physical terminals connected to a serial port (either directly or via modems) and pseudo-terminals (used by programs which emulate terminals, e.g. telnetd, xterm, screen etc).

To clear the terminal's screen (assuming that it's a video terminal; hardcopy terminals don't have a screen), you have to send the appropriate control sequence. The exact sequence depends upon the type of terminal (which can be determined from the value of the TERM environment variable).

To obtain the correct control sequence, use the terminfo library to obtain the sequence for the "clear" capability, then send that sequence to the terminal (i.e. stdout).

See the manpage for setupterm() and tigetstr() for more information.

Signature · 1 line
Glynn Clements <[email protected]>
#7 lore raw
Quoted 2 lines · Glynn Clements

See the manpage for setupterm() and tigetstr() for more information.

Should it be like this ?

<code>

    char *area[100], bp[2048] ;

    tgetent( bp, getenv( "TERM" )) ;
    puts( tgetstr( "cl", area )) ;

</code>

#8 lore raw
Quoted 11 lines · Progga
Quoted 1 line

See the manpage for setupterm() and tigetstr() for more information.

Should it be like this ?

<code>

    char *area[100], bp[2048] ;

    tgetent( bp, getenv( "TERM" )) ;
    puts( tgetstr( "cl", area )) ;

</code>

First, that's the old termcap interface; new programs should use the terminfo interface.

Second, the use of the "area" variable is wrong. It should be something like:

	char buf[100];
	char *area = buf;
	
	puts(tgetstr("cl", &area));

Repeated calls to tgetstr() will advance the "area" pointer, so that "buf" will contain, in order, all of the strings retrieved by tgetstr().

Signature · 1 line
Glynn Clements <[email protected]>