如何清除 C 语言中的控制台屏幕?
除了使用 system("cls") 之外,还有“正确”的方法来清除 C 中的控制台窗口吗?
Is there a "proper" way to clear the console window in C, besides using system("cls")
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
该功能将在 ANSI 终端上运行,需要 POSIX。我假设有一个版本也可以在 Windows 控制台上工作,因为它也支持 ANSI 转义序列。
还有一些其他的
替代方案,其中一些不会将光标移动到 {1,1}。
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}.
嗯,C 不理解屏幕的概念。所以任何代码都无法移植。也许看看 conio.h 或
curses,根据您的需要?
无论使用什么库,可移植性都是一个问题。
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.
为了可移植性,请尝试以下操作:
然后只需调用 clrscr() 即可。在 Windows 上,它将使用
conio.h
的clrscr()
,在 Linux 上,它将使用 ANSI 转义码。如果您真的想“正确”地做到这一点,您可以消除中间人(
conio
、printf
等),然后只需低级系统工具(准备大量代码转储):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):在 Windows(cmd.exe)、Linux(Bash 和 zsh)和 OS X(zsh) 上测试的解决方法:
A workaround tested on Windows(cmd.exe), Linux(Bash and zsh) and OS X(zsh):
使用宏,您可以检查您是否使用 Windows、Linux、Mac 还是 Unix,并根据当前平台调用相应的函数。事情如下:
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:
既然您提到了 cls,听起来您指的是 Windows。如果是这样,则此知识库项目具有执行此操作的代码。我刚刚尝试了一下,当我使用以下代码调用它时,它起作用了:
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:并使用
and use
Windows:
system("cls");
Unix:
system("clear");
您可以改为插入换行符,直到所有内容都滚动,看看 此处。
这样,您就可以轻松实现可移植性。
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.
没有可移植的 C 方法可以做到这一点。尽管各种光标操作库(例如curses)相对可移植。 conio.h 可在 OS/2 DOS 和 Windows 之间移植,但不能移植到 *nix变种。
“控制台”的整个概念是标准 C 范围之外的概念。
如果您正在寻找纯 Win32 API 解决方案,Windows 控制台 API 中没有单个调用可以执行此操作。一种方法是 FillConsoleOutputCharacter 足够大字符数。或者 WriteConsoleOutput 您可以使用 GetConsoleScreenBufferInfo 找出多少个字符就足够了。
您还可以创建一个全新的控制台屏幕缓冲区并制作当前的。
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 current one.
只需输入 clrscr();函数在 void main() 中。
例如:
轻松清屏功能。
just type clrscr(); function in void main().
as example:
function easy to clear screen.
在 Windows 中我犯了使用错误
,但这实际上是针对 Linux 的
Windows 类型
没有
#include conio.h
In Windows I have made the mistake of using
but that is actually for Linux
The Windows type is
without
#include conio.h
正确的方法是使用 tput 或 terminfo 函数获取终端属性,然后根据维度插入换行符。
The proper way to do it is by using
tput
orterminfo
functions to obtain terminal properties and then insert newlines according to the dimensions..这应该有效。然后只需调用 cls();每当您想清除屏幕时。
(使用之前建议的方法。)
This should work. Then just call cls(); whenever you want to clear the screen.
(using the method suggested before.)