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?
just type clrscr(); function in void main().
as example:
function easy to clear screen.
Using macros you can check if you're on Windows, Linux, Mac or Unix, and call the respective function depending on the current platform. Something as follows:
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:A workaround tested on Windows(cmd.exe), Linux(Bash and zsh) and OS X(zsh):
The proper way to do it is by using
tput
orterminfo
functions to obtain terminal properties and then insert newlines according to the dimensions..For portability, try this:
Then simply call
clrscr()
. On Windows, it will useconio.h
'sclrscr()
, and on Linux, it will use ANSI escape codes.If you really want to do it "properly", you can eliminate the middlemen (
conio
,printf
, etc.) and do it with just the low-level system tools (prepare for a massive code-dump):