Jump to content
  • Advertisement
Sign in to follow this  
Century

Clear screen command in C/C++

This topic is 5111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any possible way of clearing a screen in an MS-DOS app with C or C++? Y'know, like the old "cls" command in basic. I'm sure there must be a way, but I can't find anything that sounds like "clear" or "clear_screen", or anything like that. I don't particularly like the messiness caused by constantly scrolling text...

Share this post


Link to post
Share on other sites
Advertisement
The EASIEST way is system("cls"); which I believe is in stdlib.h... Might be mistaken on that bit though. It's not necessarily the most secure or portable, but if it's meant for DOS it will do the trick.

Share this post


Link to post
Share on other sites
That works like a charm, thanx 8)

Although, it does seem a little wayward. I mean... As far as I can tell, this is bypassing the program, and instead calling on the operating system to act on the command.

If you see what I mean.. >_>

Isn't there a more direct route for doing this?

Share this post


Link to post
Share on other sites
I think the system() command is a little overkill. What you are trying to do is control the terminal: Clear screen, locate cursor, set foreground/background colors, etc.

If you running under Linux/Unix, look at ncurses. If you running under DOS, you might try looking at ANSI escape sequences. You basically print a code, starting with ESC, that the terminal recognizes. Instead of printing the characters, it might do a CLS for instance.

Check out the clicky

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actually, if you're in unix... it's kind of overkill to use ncurses just to clear the screen...

You just do:
printf("\033[2J");

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
You just do:
printf("\033[2J");


No you don't, because that's not terminal-type independant. Your code will be far less portable due to using this.

$ man clear

Quote:
NAME
clear - clear the terminal screen

SYNOPSIS
clear

DESCRIPTION
clear clears your screen if this is possible. It looks in
the environment for the terminal type and then in the ter­
minfo database to figure out how to clear the screen.

SEE ALSO
tput(1), terminfo(5)


Thus, on *nix, use either ncurses if you want a GUI style interface - system("clear"); if you just want to clear the screen with a simple program.

On windows, either use the console control series of functions (see msdn for details) if you want a GUI style interface - system("cls"); if you just want to clear the screen with a simple program.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!