如何清除C中的屏幕?

时间:2013-08-09 19:51:08

标签: c macos unix

我想清除屏幕上的所有文字。 我尝试过使用:

#include <stdlib.h>
sys(clr);

提前致谢! 我正在使用OS X 10.6.8。抱歉混乱!

4 个答案:

答案 0 :(得分:10)

您需要查看curses.h。它是一个终端(游标)处理库,它使所有支持的文本屏幕都以类似的方式运行。

有三个已发布的版本,第三个(ncurses)是您想要的版本,因为它是最新版本,并且移植到大多数平台。 official website is here,there are a few good tutorials

#include <curses.h>

int  main(void)
{
     initscr();
     clear();
     refresh();
     endwin();
}

答案 1 :(得分:5)

清除屏幕的最佳方法是通过stdlib.h中的system(const char *command)调用shell:

system("clear"); //*nix

system("cls"); //windows

然后,最好尽量减少对调用系统/环境的函数的依赖,因为它们可能导致各种未定义的行为。

答案 2 :(得分:3)

视窗:

system("cls"); // missing 's' has been replaced

Unix的:

system("clear");

您可以将其包装在一个更易于移植的单个代码中,如下所示:

void clearscr(void)
{
#ifdef _WIN32
    system("cls");
#elif defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
    system("clear");
//add some other OSes here if needed
#else
    #error "OS not supported."
    //you can also throw an exception indicating the function can't be used
#endif
}

请注意,对unix的检查非常广泛。这也应该检测OS X,这就是你正在使用的。

答案 3 :(得分:1)

此函数或clrscn()之类的函数的可用性与系统有关,而且不可移植。

你可以保持简单并自己动手:

#include <stdio.h>

    void clearscr ( void )
    {
      for ( int i = 0; i < 50; i++ ) // 50 is arbitrary
        printf("\n");
    }
相关问题