I want to clear all the text that is on the screen. I have tried using:
#include <stdlib.h>
sys(clr);
Thanks in advance! I'm using OS X 10.6.8. Sorry for the confusion!
I want to clear all the text that is on the screen. I have tried using:
#include <stdlib.h>
sys(clr);
Thanks in advance! I'm using OS X 10.6.8. Sorry for the confusion!
You need to check out curses.h. It is a terminal (cursor) handling library, which makes all supported text screens behave in a similar manner.
There are three released versions, the third (ncurses) is the one you want, as it is the newest, and is ported to the most platforms. The official website is here, and there are a few good tutorials.
#include <curses.h>
int main(void)
{
initscr();
clear();
refresh();
endwin();
}
The best way to clear the screen is to call the shell via system(const char *command)
in stdlib.h:
system("clear"); //*nix
or
system("cls"); //windows
Then again, it's always a good idea to minimize your reliance on functions that call the system/environment, as they can cause all kinds of undefined behavior.
Windows:
system("cls"); // missing 's' has been replaced
Unix:
system("clear");
You can wrap this in a single, more portable piece of code like so:
void clearscr(void)
{
#ifdef _WIN32
system("cls");
#elif defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
system("clear");
//add some other OSes here if needed
#else
#error "OS not supported."
//you can also throw an exception indicating the function can't be used
#endif
}
Note the check for unix is pretty expansive. This should also detect OS X, which is what you're using.
The availability of this function or similar ones like clrscn() are very system dependent and not portable.
You could keep it really simple and roll you own:
#include <stdio.h>
void clearscr ( void )
{
for ( int i = 0; i < 50; i++ ) // 50 is arbitrary
printf("\n");
}