【问题标题】:Clear screen in C and C++ on UNIX-based system?在基于 UNIX 的系统上用 C 和 C++ 清除屏幕?
【发布时间】:2013-06-24 08:57:23
【问题描述】:

我想知道:如何在基于 UNIX 的系统上清理屏幕?我在网上搜索,但我刚刚找到了如何在 Windows 上进行操作:system("CLS") 我不想完全清理屏幕,但我想打开一个“新页面”,例如在 NANO 和 VI 编辑器中。谢谢

【问题讨论】:

  • vi 和 nano 使用缓冲区来保存文本数据。您将需要实现这样的缓冲区和滚动机制。
  • printf("\e[2J\e[H");

标签: c unix screen cls


【解决方案1】:

也许你可以使用转义码

#include <stdio.h>

#define clear() printf("\033[H\033[J")

int main(void)
{
    clear();
    return 0;
}

但请记住,此方法并非与所有终端兼容

【讨论】:

【解决方案2】:

您可以使用以下使用 termcap 的代码来清除屏幕。 (不要忘记与图书馆链接)

#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>

void clear_screen()
{
char buf[1024];
char *str;

tgetent(buf, getenv("TERM"));
str = tgetstr("cl", NULL);
fputs(str, stdout);
} 

【讨论】:

  • +1,在 Debian 上你必须安装 libncurses5-dev 并使用 -lncurses 编译
【解决方案3】:
#include <stdlib.h>
int main(void)
{
    system("clear");
}

【讨论】:

    【解决方案4】:

    便携式 UNIX 代码应该使用 terminfo 数据库进行所有光标和屏幕操作。这就是像curses 这样的库用来实现窗口等效果的方法。

    terminfo 数据库维护一个功能列表(如clear,您可以使用它来清除屏幕并将光标发送到顶部)。它为广泛范围的设备维护此类功能,因此您不必担心您使用的是 Linux 控制台还是(非常过时的)VT52 终端。

    至于你如何获取某些操作的字符流,你可以选择使用system的历史悠久但相当可怕的方法:

    system ("tput clear");
    

    或者您可以将该命令的输出捕获到缓冲区,以便以后使用只涉及输出字符而不是重新运行命令:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    static char scrTxtCls[20]; static size_t scrSzCls;
    
    // Do this once.
    
    FILE *fp = popen ("tput clear", "r");
    scrSzCls = fread (scrTxtCls, 1, sizeof(scrTxtCls), fp);
    pclose (fp);
    if (scrSzCls == sizeof(scrTxtCls)) {
        actIntelligently ("you may want to increase buffer size");
    }
    
    // Do this whenever you want to clear the screen.
    
    write (1, cls, clssz);
    

    或者,您可以与ncurses 链接并使用它的API 来获得您想要的任何功能,尽管这可能会拖入相当多的东西,比如清除屏幕这样简单的事情。不过,这是一个值得认真考虑的选项,因为它为您提供了更多的灵活性。

    【讨论】:

      【解决方案5】:

      这通常不仅仅是清除屏幕的问题,而是制作一个终端感知应用程序。

      您应该使用ncurses 库并阅读NCURSES programming HowTo

      (您也许可以使用一些ANSI escape codes 作为David RF 的回答,但我认为这不是一个好主意)

      【讨论】:

        【解决方案6】:

        您可以使用 CSI 序列实现此目的:

        #include <stdio.h>
        int main()
        {
            printf("\x1b[H\x1b[J");
        }
        

        \x1b[H 是什么意思?

        其实和\x1b[1;1;H一样,表示会将光标移动到第1行第1列。

        \x1b[J 又名 \x1b[0;J 是什么意思?

        如果 n 为 0 或缺失,它将从光标到屏幕末尾清除。

        来源:https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences

        【讨论】:

        • \x1b[2J 不足以移动光标和清屏吗?
        【解决方案7】:

        只需在#include&lt;stdio.h&gt; 之后使用#include&lt;stdlib.h&gt;

        然后你可以在main() {之后使用命令system("clear");

        即:

        #include<stdio.h>
        #include<stdlib.h>
        
        int main()
        {
            system("clear");
        

        执行这些命令后,您可以继续执行您的程序。

        希望这会有所帮助:)

        【讨论】:

          【解决方案8】:

          要使用 termcaps 清除屏幕,请使用:

          write(1, tgetstr("cl", 0), strlen(tgetstr("cl", 0)));
          

          【讨论】:

            【解决方案9】:

            system("clear"); 与标题#include &lt;stdlib.h&gt;(用于C 语言)或#include &lt;cstdlib&gt;(用于C++)一起使用。

            【讨论】:

            • 这是你应该尝试的。
            【解决方案10】:

            此代码用于在终端样式窗口中重置滚动条位置的清晰屏幕

            #include <iostream>
            
            int main(){
               std::cout << "\033c";
               return 0;
            }
            

            【讨论】:

              猜你喜欢
              • 2011-10-01
              • 2013-06-24
              • 2018-12-20
              • 2020-07-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-06-28
              • 1970-01-01
              相关资源
              最近更新 更多