Page 1 of 1

Portable way to clear a screen in C++

Posted: 2004-03-11 12:33am
by Durandal
This is pissing me off. I've been searching forever, and I haven't found a cross-platform method for clearing the screen using C++. My C++ teacher gave us system ("cls"), since apparently no other compilers exist in the scope of this class.

I could use system ("clear") for for *nix boxes, but I'd rather just have one set of code for the trivial programs I'm writing. Anyone know of a way to clear the screen that will work under VS.Net and gcc or any compliant C compiler?

Posted: 2004-03-11 01:18am
by Pu-239
I don't think there are any, since I haven't found it anywhere in the standard C library. Can't you use #defines, #ifdef, #else, and #endif, and/or make a wrapper function?

Posted: 2004-03-11 03:38am
by Sarevok
PU has a good point there. The best way would be to use a macro.

Posted: 2004-03-11 01:42pm
by Slartibartfast
Using the system() is probably the worst way to go about it. The computer effectively has to run a new process just to clear your screen... in DOS it means loading a COMMAND.COM in memory, executing "cls", then terminating COMMAND.COM.

The best way is to directly write in screen memory, and yes, that's pretty much machine-specific. There are a few tutorials on the 'net. Some consoles may not even HAVE a clear function (specifically dumb terminals I suppose), and on those you just have to do a page break or something - that is, writing 25 or so line feeds.

Maybe you can try putchar(12) or putchar(14) or sumthin.

To tell the truth, if you're trying to do it in a portable way, there is no real reason to clear the screen, just as there should be no reason to use extended ASCII characters, or colored chars, etc... like if you're trying to make some kind of DOS console function (like "arj x file.arj")

Posted: 2004-03-11 07:26pm
by Pu-239
The problem is that different characters are written to different consoles, such as a VT or an Xterm, so you'll have to account for that too if you don't use clear. It's better on *nix just to call functions from your curses library. Don't know much about DOS.


Funny how I made a JNI class that called a native library that simply ran system(cls) for cls() that was called every 20ms- horribly inefficient, but easy.


For ncurses, you do :

Code: Select all

#include <curses.h>
#include <term.h>
                                                                                                                                              
int main(void){
    setupterm((char *)0, 1, (int *)0);
    putp(clear_screen);
    endwin();
    return 0;
}
Note the above was derieved from reading the various ncurses man pages, which are a bitch(possibily resulting in bad coding).

Posted: 2004-03-11 07:48pm
by Jawawithagun
the cheapest and easiest way to do it usually is just printing 25 carriage returns, scrolling the onscreen text up 25 lines (and therefore clearing the screen in a 80x25 console window as it is common under WinDos)

Posted: 2004-03-11 08:28pm
by Pu-239
Except that *nix consoles aren't always a puny 25 lines like Windoze... :lol: mine's 160x64 on VT and 143x69 in maximized terminal emulator (yes, I know you can change the # of lines on a cmd console, but I don't think you can change columns).

Posted: 2004-03-11 08:30pm
by Pu-239
Also, on *nix, the ncurses method is cheaper, since all it does is print a couple characters to trigger a clearscreen rather than attempting to scroll down or overwrite stuff w/ blanks.

Posted: 2004-03-12 12:23am
by Durandal
Jawawithagun wrote:the cheapest and easiest way to do it usually is just printing 25 carriage returns, scrolling the onscreen text up 25 lines (and therefore clearing the screen in a 80x25 console window as it is common under WinDos)
*nix consoles can be a lot larger than 80x24.

Posted: 2004-03-12 12:47am
by Uraniun235
Clearly the answer is to put in 500 carriage returns. :P

Posted: 2004-03-12 09:40am
by Mad
The man page for clear:
The clear utility clears the terminal screen if this is possible. It looks in the environment for the terminal type, if this is not already specified by the term operand, and then looks up the terminfo database to figure out how to clear the screen.
Essentially, that's what you'd have to do to do it properly for *nix. To take a shortcut, you'd probably just take the codes used for the most common term types (xterm, vt100, and ansi, I think are the 3 most common).

For MS-DOS/Windows console, I know there used to be a control character you could send that would clear the screen. Dunno if it became unsupported in newer incarnations, though. Try outputting char values between from 1-31 and see what you get.

Posted: 2004-03-12 12:12pm
by Durandal
I just settled on this ...

Code: Select all

void clearScreen ()
{
	#if defined (WIN_32)
		system ("cls");
	#else
		system ("clear");
	#endif
}

Posted: 2004-03-12 06:37pm
by Pu-239
Mad wrote:The man page for clear:
The clear utility clears the terminal screen if this is possible. It looks in the environment for the terminal type, if this is not already specified by the term operand, and then looks up the terminfo database to figure out how to clear the screen.
Essentially, that's what you'd have to do to do it properly for *nix. To take a shortcut, you'd probably just take the codes used for the most common term types (xterm, vt100, and ansi, I think are the 3 most common).

For MS-DOS/Windows console, I know there used to be a control character you could send that would clear the screen. Dunno if it became unsupported in newer incarnations, though. Try outputting char values between from 1-31 and see what you get.

Well that's what this did:

Code: Select all

#include <curses.h>
#include <term.h>
                                                                                                                                             
int main(void){
    setupterm((char *)0, 1, (int *)0);
    putp(clear_screen);
    endwin();
    return 0;
}
putp just reads what you want to send it and prints out the necessary character from the terminfo database. No need to figure out how to query the TI database manually.