Portable way to clear a screen in C++
Moderator: Thanas
- Durandal
- Bile-Driven Hate Machine
- Posts: 17927
- Joined: 2002-07-03 06:26pm
- Location: Silicon Valley, CA
- Contact:
Portable way to clear a screen in C++
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?
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?
Damien Sorresso
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
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?
ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer
George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
- Slartibartfast
- Emperor's Hand
- Posts: 6730
- Joined: 2002-09-10 05:35pm
- Location: Where The Sea Meets The Sky
- Contact:
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")
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")
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 :
Note the above was derieved from reading the various ncurses man pages, which are a bitch(possibily resulting in bad coding).
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;
}
Last edited by Pu-239 on 2004-03-11 07:54pm, edited 1 time in total.
ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer
George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
- Jawawithagun
- Jedi Master
- Posts: 1141
- Joined: 2002-10-10 07:05pm
- Location: Terra Secunda
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)
"I said two shot to the head, not three." (Anonymous wiretap, Dallas, TX, 11/25/63)
Only one way to make a ferret let go of your nose - stick a fag up its arse!
there is no god - there is no devil - there is no heaven - there is no hell
live with it
- Lazarus Long
Only one way to make a ferret let go of your nose - stick a fag up its arse!
there is no god - there is no devil - there is no heaven - there is no hell
live with it
- Lazarus Long
Except that *nix consoles aren't always a puny 25 lines like Windoze... 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).
ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer
George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
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.
ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer
George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
- Durandal
- Bile-Driven Hate Machine
- Posts: 17927
- Joined: 2002-07-03 06:26pm
- Location: Silicon Valley, CA
- Contact:
*nix consoles can be a lot larger than 80x24.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)
Damien Sorresso
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
- Uraniun235
- Emperor's Hand
- Posts: 13772
- Joined: 2002-09-12 12:47am
- Location: OREGON
- Contact:
- Mad
- Jedi Council Member
- Posts: 1923
- Joined: 2002-07-04 01:32am
- Location: North Carolina, USA
- Contact:
The man page for clear:
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.
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).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.
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.
Later...
- Durandal
- Bile-Driven Hate Machine
- Posts: 17927
- Joined: 2002-07-03 06:26pm
- Location: Silicon Valley, CA
- Contact:
I just settled on this ...
Code: Select all
void clearScreen ()
{
#if defined (WIN_32)
system ("cls");
#else
system ("clear");
#endif
}
Damien Sorresso
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
Mad wrote:The man page for clear:
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).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.
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;
}
ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer
George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor