使用 C++ 清除屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17335816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear Screen using C++
提问by Glen Morse
I would like to clear the CMD screen I have seen a few options first is
我想清除 CMD 屏幕我首先看到的几个选项是
system('clr');
but dont want to use system cause then it makes it dependent on windows, same with the unix version
但不想使用系统原因然后它依赖于windows,与unix版本相同
if i try
如果我尝试
cout << string(22, '\n');
then my next line of text is at the bottom of the screen and i want it at the top. How can I clear the screen and get the text back to the top of the screen?
然后我的下一行文本位于屏幕底部,我希望它位于顶部。如何清除屏幕并使文本回到屏幕顶部?
Thus say I have this.
因此说我有这个。
cout<<string(22, '\n');
cout<<"************Question 1 *******"<<endl;
cout<<"WHO WAS THE FIRST BLACK PRESEDENT?"<<endl;
cout<<"(1) Obama"<<endl;
cout<<"(2) Bush"<<endl;
cout<<"(3) Jordan" <<endl;
cin>>answer>>endl;
this will clear the screen then put mymenu at the bottom of the screen... how can i make it clear screen and put the question/answers back up top of screen.
这将清除屏幕,然后将 mymenu 放在屏幕底部...我如何才能清除屏幕并将问题/答案放回屏幕顶部。
回答by catzilla
Try this: it works both on Linux and Windows.
试试这个:它适用于 Linux 和 Windows。
cout << "3[2J3[1;1H";
This is a string of special characters that translate to clear the screen command.
这是一串特殊字符,可转换为清除屏幕命令。
You can enclose this in a function like e.g. clrscr()
depending on your implementation.
您可以clrscr()
根据您的实现将其包含在一个函数中,例如。
回答by CmdrMoozy
If you want a solution that will work on Windows, Mac & Linux/UNIX, you will need to come up with your own implementation. I do not believe that there is a single way to do it that works on all platforms.
如果您想要一个适用于 Windows、Mac 和 Linux/UNIX 的解决方案,您将需要提出自己的实施方案。我不相信有一种方法可以在所有平台上运行。
For Mac/Linux/UNIX/BSD/etc., ncurses provides an easy way to do this (http://www.gnu.org/software/ncurses/).
对于 Mac/Linux/UNIX/BSD/等,ncurses 提供了一种简单的方法来做到这一点 ( http://www.gnu.org/software/ncurses/)。
For Windows, you will probably want to look into conio.h
(http://en.wikipedia.org/wiki/Conio.h) or PDCurses (http://pdcurses.sourceforge.net/) or something similar. Alternatively, it would seem that you can do this without any third-party libraries, according to this Microsoft KB article: http://support.microsoft.com/kb/99261.
对于 Windows,您可能需要查看conio.h
( http://en.wikipedia.org/wiki/Conio.h) 或 PDCurses ( http://pdcurses.sourceforge.net/) 或类似内容。或者,根据这篇 Microsoft 知识库文章:http: //support.microsoft.com/kb/99261,您似乎可以在没有任何第三方库的情况下执行此操作。
There is unfortunately no standard C/C++ function to do this. You should be able to write a small function which will build & work on any platform using the different methods I mentioned and some preprocessor directives.
不幸的是,没有标准的 C/C++ 函数可以做到这一点。您应该能够编写一个小函数,该函数可以使用我提到的不同方法和一些预处理器指令在任何平台上构建和运行。
If you don't have a convenient way to detect the platform, I would probably recommend cmake.
如果您没有方便的方法来检测平台,我可能会推荐 cmake。
回答by user1095108
Another way would be to use OpenGL, Qt or SDL, which are cross-platform and write a graphical console. This can be seen in many roguelike games, for example Dwarf Fortress.
另一种方法是使用 OpenGL、Qt 或 SDL,它们是跨平台的并编写图形控制台。这可以在许多 roguelike 游戏中看到,例如矮人要塞。
回答by Aswin Murugesh
In UNIX try
在 UNIX 中尝试
system("clear")
clrscr()
may not work in UNIX because some compilers do not support conio.h
clrscr()
可能无法在 UNIX 中工作,因为某些编译器不支持 conio.h