Is there a "proper" way of clearing a screen in C for a console application?
(Besides using:system("cls")
)
views:
570answers:
6Well, C doesn't understand the concept of screen. So any code would fail to be portable.
Maybe take a look at conio.h or curses, according to your needs?
Portability is an issue, no matter the library used.
The proper way to do it is by using tput
or terminfo
functions to obtain terminal properties and then insert newlines according to the dimensions..
Since you mention cls
, it sounds like you are referring to windows. If so, then this KB item has the code that will do it. I just tried it, and it worked when I called it with the following code:
cls( GetStdHandle( STD_OUTPUT_HANDLE ));
Windows:
system("cls");
Unix:
system("clear");
You could instead, insert newline chars until everything gets scrolled, take a look here.
With that, you achieve portability easily.
There is no C portable way to do this. Although various cursor manipulation libraries like curses are relatively portable. conio.h is portable between OS/2 DOS and Windows, but not to *nix variants.
The entire notion of a "console" is a concept outside of the scope of standard C.
If you are looking for a pure Win32 API solution, There is no single call in the Windows console API to do this. One way is to FillConsoleOutputCharacter of a sufficiently large number of characters. Or WriteConsoleOutput You can use GetConsoleScreenBufferInfo to find out how many characters will be enough.
You can also create an entirely new Console Screen Buffer and make the the current one.