PDA

View Full Version : To clear a screan


usha
12-01-2005, 07:08 AM
Hai,
I am unable to clear the screen in between a program.i have written the code below.Please see it.
main()
{
printf("hello \n");
clrscr();
printf("hai\n");
}[COLOR=YellowGreen]

I included the headers stdio.h,stdlib.h,termios.h,curses.h......
I also wanted to know how to use fflush in linux.Please give any sample codes for these two so that i can clear the screen .I gave fflush(stdin).But it is not clearing the screen.
Thanking u
usha

i3839
12-01-2005, 08:52 AM
Try printf("\33c"), that's an ANSI escape sequence which should be reasonably portable.

fflush() is only to flush all pending data to a file or screen. With line buffered things like terminals all data is flushed automaically after each \n, so in general you don't need to use fflush ()at all.

RobSeace
12-01-2005, 01:50 PM
Yeah, most terminals will recognize ANSI escape sequences... But, I can certainly
find you some that won't... (For utterly inconceivable reasons, QNX decided to use
0xff chars as their 'escape' char in all terminal sequences, rather than an actual
escape char... So, no standard ANSI stuff has a prayer of ever working on a pure
QNX terminal... They do have an ANSI mode they can be switched into though,
thankfully... Which, of course, only further confuses me as to why the hell they
didn't make that the standard mode instead of the insane 0xff mode, in the first
damn place! ;-))

But, if you need to do stuff like clear the screen, you're probably better off using
curses/ncurses, and being sure to do things in a portable, terminal-independent
way... It looks like you were TRYING to use curses, in that you included curses.h,
but I'm not sure where you got clrscr() from; I think the function you're looking for
is clear()... But, in order to use it, you first need to setup curses for use, typically
by calling initscr()... And, typically, curses automatically clears the screen after
an initscr() anyway, so all you really need to do is initscr(), refresh(), and then
endwin()...

i3839
12-01-2005, 03:01 PM
Using curses only to clear a screen seems like overkill to me. If you want to also do other, more interesting tings then yes, curses is the way to go.

RobSeace
12-01-2005, 06:38 PM
Yes, it's overkill, but it's probably the quickest thing to do... The alternative (if you
cared about non-ANSI-compatible terminals, which most people probably wouldn't
anyway) would be to just load the terminfo/termcap properties for the terminal (via
setupterm() or tgetent()), lookup the proper sequence for the "clear"/"cl" entry, and
then use tputs() to output that...