如何使用 C++ 清除 Windows 和 Linux 中的控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/228617/
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
How do I clear the console in BOTH Windows and Linux using C++
提问by Athena
I need a cross platform solution for clearing the console in both Linux and Windows written in C++. Are there any functions in doing this? Also make note that I don't want the end-user programmer to have to change any code in my program to get it to clear for Windows vs Linux (for example if it has to pick between two functions then the decision has to be made at run-time or at compile-time autonomously).
我需要一个跨平台的解决方案来清除用 C++ 编写的 Linux 和 Windows 中的控制台。这样做有什么功能吗?还要注意,我不希望最终用户程序员必须更改我的程序中的任何代码才能使其在 Windows 和 Linux 中被清除(例如,如果它必须在两个函数之间进行选择,则必须做出决定在运行时或在编译时自主)。
采纳答案by coppro
Short answer: you can't.
简短的回答:你不能。
Longer answer: Use a curses library (ncurses on Unix, pdcurseson Windows). NCurses should be available through your package manager, and both ncurses and pdcurses have the exact same interface (pdcurses can also create windows independently from the console that behave like console windows).
更长的答案:使用curses库(Unix上的ncurses,Windows上的pdcurses)。NCurses 应该可以通过您的包管理器使用,并且 ncurses 和 pdcurses 都具有完全相同的界面(pdcurses 也可以独立于控制台创建窗口,其行为类似于控制台窗口)。
Most difficult answer: Use #ifdef _WIN32
and stuff like that to make your code act differently on different operating systems.
最困难的答案:使用#ifdef _WIN32
和类似的东西让你的代码在不同的操作系统上表现不同。
回答by Athena
I know this isn't answering my own question but!
This works for Windows (#include <windows.h>
):
我知道这不是在回答我自己的问题,但是!这适用于 Windows ( #include <windows.h>
):
void clrscr()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
}
回答by Adam Pierce
This is how you do it on any other platform but it doesn't work in Windows:
这是您在任何其他平台上执行此操作的方式,但它在 Windows 中不起作用:
cout << "\f";
Perhaps you'll need to make a conditional compilation:
也许您需要进行条件编译:
void clrscr()
{
#ifdef _WIN32
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ',
csbi.dwSize.X * csbi.dwSize.Y,
coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
#else
cout << "\f";
#endif
}
回答by fmsf
There is no generic command to clear the console on both platforms.
#include <cstdlib>
void clear_screen()
{
#ifdef WINDOWS
std::system("cls");
#else
// Assume POSIX
std::system ("clear");
#endif
}
回答by Nicola Bonelli
On linux it's possible to clear the console. The finest way is to write the following escape sequence to stdout:
在 linux 上可以清除控制台。最好的方法是将以下转义序列写入标准输出:
write(1,"\E[H\E[2J",7);
which is what /usr/bin/cleardoes, without the overhead of creating another process.
这就是/usr/bin/clear所做的,没有创建另一个进程的开销。
回答by JdeBP
The question as posted is unanswerable, because it imposes impossible restrictions. "Clearing the screen" is a very different action across different operating systems, and how one does it isoperating system specific. See this Frequently Given Answerfor a full explanation of how to do it on several popular platforms with "consoles" and platforms with "terminals". You'll also find in the same placesome explanation of the common mistakes to avoid, several of which are — alas! — given above as answers.
发布的问题无法回答,因为它施加了不可能的限制。“清除屏幕”是跨不同操作系统的一项非常不同的操作,具体操作方式也因操作系统而异。有关如何在具有“控制台”的几个流行平台和具有“终端”的平台上执行此操作的完整说明,请参阅此经常给出的答案。您还会在同一个地方找到一些对要避免的常见错误的解释,其中有几个是 - 唉!- 以上作为答案给出。
回答by fatpugsley
Wouldn't
不会
for (int i=0;i<1000;i++){cout<<endl;}
clear the screen in all OSes?
在所有操作系统中清除屏幕?
回答by Nick
A simple trick: Why not checking the OS type by using macros in combination with using the system() command for clearing the console? This way, you are going to execute a system command with the appropriate console command as parameter.
一个简单的技巧:为什么不通过使用宏结合使用 system() 命令来清除控制台来检查操作系统类型?这样,您将使用适当的控制台命令作为参数来执行系统命令。
#ifdef _WIN32
#define CLEAR "cls"
#else //In any other OS
#define CLEAR "clear"
#endif
//And in the point you want to clear the screen:
//....
system(CLEAR);
//....
回答by Questionare232
Well there is a very close alternative to clearing the screen. You could try using a for loop that repeats new lines a lot. For example:
那么有一个非常接近的选择来清除屏幕。您可以尝试使用重复新行的 for 循环。例如:
for (i = 0; i < 100000; i++)
{
printf ("\n\n\n\n\n");
}
After you do this loop the terminal wan't allow you to scroll back to where you were at the top, an unprofessional approach with common sense pretty much. It does not directly answer what you are asking but it can work.
执行此循环后,终端不允许您滚动回顶部的位置,这是一种非常具有常识的不专业方法。它不会直接回答您的问题,但它可以工作。
回答by fidelmcscort
This should work if you're working on console
如果您在控制台上工作,这应该可以工作
#include <conio.h>
int main()
{
clrscr();
}