如何使用c++清除Windows和Linux中的控制台

时间:2022-03-11 20:47:44

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之间(例如如果选择两个函数在运行时必须做出决定或者在编译时自动)。

12 个解决方案

#1


28  

Short answer: you can't.

简短的回答是:你不能。

Longer answer: Use a curses library (ncurses on Unix, pdcurses on 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).

更长的回答:使用一个诅咒库(Unix上的ncurses, Windows上的pdcurses)。NCurses应该可以通过您的包管理器获得,并且NCurses和pdcurses具有完全相同的接口(pdcurses也可以独立于控制台创建windows,其行为类似控制台windows)。

Most difficult answer: Use #ifdef _WIN32 and stuff like that to make your code act differently on different operating systems.

最困难的回答是:使用#ifdef _WIN32之类的东西使您的代码在不同的操作系统上有不同的表现。

#2


51  

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
}

#3


13  

On linux it's possible to clear the console. The finest way is to write the following escape sequence to stdout:

在linux上,可以清除控制台。最好的方法是将以下转义序列写入stdout:

write(1,"\E[H\E[2J",7);

which is what /usr/bin/clear does, without the overhead of creating another process.

这就是/usr/bin/clear所做的,无需创建另一个流程的开销。

#4


10  

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.

一个简单的技巧:为什么不使用宏和系统()命令来检查操作系统?通过这种方式,您将使用适当的控制台命令作为参数来执行系统命令。

#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);
//....

#5


4  

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 is operating system specific. See this Frequently Given Answer for 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 place some explanation of the common mistakes to avoid, several of which are — alas! — given above as answers.

所提出的问题是无法回答的,因为它强加了不可能的限制。“清除屏幕”是跨不同操作系统的一个非常不同的操作,其具体操作方式与操作系统有关。有关如何在几个流行的平台上使用“控制台”和使用“终端”的平台上使用它的完整解释,请参阅这个经常给出的答案。你还会在同一个地方找到一些常见错误的解释,其中有几个是——唉!-以上给出的答案。

#6


3  

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
}

#7


2  

I know this isn't answering my own question but! This works for Windows (#include <windows.h>):

我知道这不是在回答我自己的问题,但是!这适用于Windows(#包含< 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);
}

#8


2  

Short answer

简短的回答

void cls(void)
{
    system("cls||clear");
    return;
}

Long answer, please read:

长回答,请阅读:

system("pause") clarification

系统(“暂停”)澄清

#9


1  

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.

在您执行此循环之后,终端wan不允许您回滚到您在顶部的位置,这几乎是一种具有常识的非专业方法。它不能直接回答你的问题,但它可以工作。

#10


1  

This code clears the console in BOTH Windows and Unix (Although it's actually compiled differently):

这段代码清除了Windows和Unix中的控制台(尽管实际上编译方式不同):

// File: clear_screen.h
#ifndef _CLEAR_SCREEN_H
#define _CLEAR_SCREEN_H
void clearScreen(void); /* Clears the screen */
#endif /* _CLEAR_SCREEN_H */
// File: clear_screen.c
#ifdef _WIN32
#include <windows.h>
void clearScreen(void) {
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD topLeft = {0, 0};
    DWORD dwCount, dwSize;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hOutput, &csbi);
    dwSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hOutput, 0x20, dwSize, topLeft, &dwCount);
    FillConsoleOutputAttribute(hOutput, 0x07, dwSize, topLeft, &dwCount);
    SetConsoleCursorPosition(hOutput, topLeft);
}
#endif /* _WIN32 */

#ifdef __unix__
#include <stdio.h>
void clearScreen(void) {
    printf("\x1B[2J");
}
#endif /* __unix__ */

#11


-2  

Wouldn't

不会

for (int i=0;i<1000;i++){cout<<endl;}

clear the screen in all OSes?

清除所有操作系统中的屏幕?

#12


-3  

This should work if you're working on console

如果您正在使用控制台,这应该可以工作

#include <conio.h>

int main()

{
    clrscr();
}

#1


28  

Short answer: you can't.

简短的回答是:你不能。

Longer answer: Use a curses library (ncurses on Unix, pdcurses on 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).

更长的回答:使用一个诅咒库(Unix上的ncurses, Windows上的pdcurses)。NCurses应该可以通过您的包管理器获得,并且NCurses和pdcurses具有完全相同的接口(pdcurses也可以独立于控制台创建windows,其行为类似控制台windows)。

Most difficult answer: Use #ifdef _WIN32 and stuff like that to make your code act differently on different operating systems.

最困难的回答是:使用#ifdef _WIN32之类的东西使您的代码在不同的操作系统上有不同的表现。

#2


51  

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
}

#3


13  

On linux it's possible to clear the console. The finest way is to write the following escape sequence to stdout:

在linux上,可以清除控制台。最好的方法是将以下转义序列写入stdout:

write(1,"\E[H\E[2J",7);

which is what /usr/bin/clear does, without the overhead of creating another process.

这就是/usr/bin/clear所做的,无需创建另一个流程的开销。

#4


10  

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.

一个简单的技巧:为什么不使用宏和系统()命令来检查操作系统?通过这种方式,您将使用适当的控制台命令作为参数来执行系统命令。

#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);
//....

#5


4  

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 is operating system specific. See this Frequently Given Answer for 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 place some explanation of the common mistakes to avoid, several of which are — alas! — given above as answers.

所提出的问题是无法回答的,因为它强加了不可能的限制。“清除屏幕”是跨不同操作系统的一个非常不同的操作,其具体操作方式与操作系统有关。有关如何在几个流行的平台上使用“控制台”和使用“终端”的平台上使用它的完整解释,请参阅这个经常给出的答案。你还会在同一个地方找到一些常见错误的解释,其中有几个是——唉!-以上给出的答案。

#6


3  

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
}

#7


2  

I know this isn't answering my own question but! This works for Windows (#include <windows.h>):

我知道这不是在回答我自己的问题,但是!这适用于Windows(#包含< 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);
}

#8


2  

Short answer

简短的回答

void cls(void)
{
    system("cls||clear");
    return;
}

Long answer, please read:

长回答,请阅读:

system("pause") clarification

系统(“暂停”)澄清

#9


1  

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.

在您执行此循环之后,终端wan不允许您回滚到您在顶部的位置,这几乎是一种具有常识的非专业方法。它不能直接回答你的问题,但它可以工作。

#10


1  

This code clears the console in BOTH Windows and Unix (Although it's actually compiled differently):

这段代码清除了Windows和Unix中的控制台(尽管实际上编译方式不同):

// File: clear_screen.h
#ifndef _CLEAR_SCREEN_H
#define _CLEAR_SCREEN_H
void clearScreen(void); /* Clears the screen */
#endif /* _CLEAR_SCREEN_H */
// File: clear_screen.c
#ifdef _WIN32
#include <windows.h>
void clearScreen(void) {
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD topLeft = {0, 0};
    DWORD dwCount, dwSize;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hOutput, &csbi);
    dwSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hOutput, 0x20, dwSize, topLeft, &dwCount);
    FillConsoleOutputAttribute(hOutput, 0x07, dwSize, topLeft, &dwCount);
    SetConsoleCursorPosition(hOutput, topLeft);
}
#endif /* _WIN32 */

#ifdef __unix__
#include <stdio.h>
void clearScreen(void) {
    printf("\x1B[2J");
}
#endif /* __unix__ */

#11


-2  

Wouldn't

不会

for (int i=0;i<1000;i++){cout<<endl;}

clear the screen in all OSes?

清除所有操作系统中的屏幕?

#12


-3  

This should work if you're working on console

如果您正在使用控制台,这应该可以工作

#include <conio.h>

int main()

{
    clrscr();
}