Is there a "proper" way to clear the console window in C, besides using system("cls")
?
A file pointer is used to open and read the file. It is displaying the content of file. To clear the console, clrscr() is used.
You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators. If you use Ctrl+L and clear command in GNOME terminal (default in Ubuntu), you'll notice the difference between their impact.
printf("\e[1;1H\e[2J");
This function will work on ANSI terminals, demands POSIX. I assume there is a version that might also work on window's console, since it also supports ANSI escape sequences.
#include <unistd.h> void clearScreen() { const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J"; write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12); }
There are some other alternatives, some of which don't move the cursor to {1,1}.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With