简体   繁体   English

在 C++ 中清除控制台

[英]Clearing the Console in C++

I am trying to clear the console in C++.我正在尝试清除 C++ 中的控制台。 I know printing a number of newlines is a bad practice, as it can be slow and is not always reliable to completely clear the console window, but I have researched multiple options and have found almost no other solutions besides system("cls") , which is an even worse option.我知道打印多个换行符是一种不好的做法,因为它可能很慢并且完全清除控制台窗口并不总是可靠的,但是我研究了多个选项并且发现除了system("cls")之外几乎没有其他解决方案,这是一个更糟糕的选择。

Essentially, I have used the line cout << string(100, '\\n');本质上,我使用了行cout << string(100, '\\n'); but I am getting a near-unidentifiable error when I try to run the program.但是当我尝试运行该程序时,我遇到了一个几乎无法识别的错误。

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

I have also researched this, and found that most explanations were too complicated for me as a beginning C++ programmer to understand, or completely unrelated to my problem.我也研究过这个,发现大部分解释对我这个初学 C++ 程序员来说太复杂了,无法理解,或者与我的问题完全无关。

My questions are (1) is there a way to fix this error, and (2) could there be a better, cross-platform way of clearing the console other than printing 100 newlines?我的问题是 (1) 有没有办法解决这个错误,以及 (2) 除了打印 100 个换行符之外,还有没有更好的跨平台方式来清除控制台?

I also heard of Console.clear() , but I'm unsure if this is cross-platform.我也听说过Console.clear() ,但我不确定这是否是跨平台的。 From what I've seen, it looks more like a Windows command.从我所见,它看起来更像是一个 Windows 命令。 I've also heard of the curses library, which I was willing to research and use, until I read somewhere that it was not recommended to use the functions which I am familiar with coupled with the curses library functions.我也听说过curses库,我很愿意研究和使用它,直到我在某处读到不建议将我熟悉的函数与curses 库函数结合使用。

Thank you in advance!先感谢您!

At a guess, your immediate problem is probably that you're missing an #include <string> .猜测,您的直接问题可能是您缺少一个#include <string>

Probably the most portable way of dealing with the screen is via ncurses.处理屏幕的最便携方式可能是通过 ncurses。 It's included in POSIX and most POSIX-like systems, and available as a library for most others (eg, Windows) as well.它包含在 POSIX 和大多数类似 POSIX 的系统中,也可以作为大多数其他系统(例如,Windows)的库使用。

Edit: For what it's worth, clearing the screen on Windows doesn't require anywhere close to 100 lines of code.编辑:就其价值而言,在 Windows 上清除屏幕不需要接近 100 行代码的任何地方。

#include <windows.h>

void clear_screen(char fill = ' ') { 
    COORD tl = {0,0};
    CONSOLE_SCREEN_BUFFER_INFO s;
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);   
    GetConsoleScreenBufferInfo(console, &s);
    DWORD written, cells = s.dwSize.X * s.dwSize.Y;
    FillConsoleOutputCharacter(console, fill, cells, tl, &written);
    FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
    SetConsoleCursorPosition(console, tl);
}

#ifdef TEST
int main(){ 
    clear_screen();
    return 0;
}
#endif

I'm the first to say that the code is more verbose than I'd like -- but it's less than ten lines, not to mention a hundred.我是第一个说代码比我想要的更冗长的人——但它不到十行,更不用说一百行了。 Even the version in the MS knowledgebase is actually less than 40 lines -- of which many are blank or comments.甚至 MS 知识库中的版本实际上也少于 40 行——其中许多是空白或注释。

In fairness, however, I feel obliged to admit assembly language code writing directly to the hardware (or using the BIOS) does end up quite a bit shorter.然而,公平地说,我不得不承认直接将汇编语言代码写入硬件(或使用 BIOS)确实会缩短很多时间。

About your error... you have to...关于你的错误......你必须......

#include <iostream> 
#include <string>

using namespace std;

If you are using just windows use windows console API.如果您只使用 Windows,请使用 Windows 控制台 API。 If you are using a linux\\unix terminal, use escape codes.如果您使用的是 linux\\unix 终端,请使用转义码。 You can do a #if to choose between the two methods.您可以执行 #if 以在两种方法之间进行选择。

On linux\\unix use the write function defined in in this way:在 linux\\unix 上使用以这种方式定义的 write 函数:

write(1,"\E[H\E[2J",7); // we use ANSI escape sequences here.

Here is the microsoft page that explain how to do that.这是解释如何做到这一点的微软页面。

http://support.microsoft.com/kb/99261 http://support.microsoft.com/kb/99261

The really bad console api microsoft use for the console always makes me angry :) why 100 lines of code to clear a screen?微软用于控制台的非常糟糕的控制台 api 总是让我生气:) 为什么 100 行代码来清除屏幕? :) :)

Now the if... you should create a clearscreen.h file and a clearscreen.cpp file.现在,如果...您应该创建一个 clearscreen.h 文件和一个 clearscreen.cpp 文件。

In clearscreen.h we just put our function.在 clearscreen.h 中,我们只放置了我们的函数。

 void clearconsole();

In clearscreen.cpp we put our code for both operative systems在 clearscreen.cpp 中,我们放置了两个操作系统的代码

#ifdef _WIN32 || _WIN64

    #include <windows.h>

    void clearconsole()
    {
        ...
        // 100 lines of codes copied from microsoft article
    }

#else

    #include <unistd.h>

    void clearconsole()
    {
        write(1,"\E[H\E[2J",7);
    }

#endif

I know this is a complete necro.我知道这是一个完整的死灵。 But I figured out what I feel is a rather neat solution and thought I'd share it just in case someone has this problem in the future.但我发现我觉得这是一个相当简洁的解决方案,并认为我会分享它,以防将来有人遇到这个问题。

void clearConsole() {
#ifdef _WIN32
#include <iostream>
    static const char* CSI = "\33[";
    printf("%s%c%s%c", CSI, 'H', CSI, '2J');

#else
#include <unistd.h>
    write(1, "\E[H\E[2J", 7);
#endif
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM