Is there a "proper" way to clear the console window in C, besides using system("cls")
?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- Inheritance impossible in Windows Runtime Componen
- glDrawElements only draws half a quad
- how to get running process information in java?
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.
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.
There are some other alternatives, some of which don't move the cursor to {1,1}.
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.
Well, 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 what library is used.
This should work. Then just call cls(); whenever you want to clear the screen.
(using the method suggested before.)
and use