• java 显示c控制台程序窗口,Windows-如何清除C中的控制台屏幕?


    Windows-如何清除C中的控制台屏幕?

    除了使用system("cls")之外,是否还有“适当”的方法来清除C中的控制台窗口?

    12个解决方案

    26 votes

    printf("\e[1;1H\e[2J");

    此功能将在ANSI终端上工作,需要POSIX。 我假设有一个版本也可以在窗口的控制台上使用,因为它还支持ANSI转义序列。

    #include

    void clearScreen()

    {

    const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";

    write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12);

    }

    还有一些替代方案,其中某些方案不会将光标移至{1,1}。

    Avinash Katiyar answered 2020-02-02T13:49:13Z

    24 votes

    好吧,C不理解屏幕的概念。 因此,任何代码都无法移植。 也许看看conio.h或诅咒,根据您的需要?

    无论使用什么库,可移植性都是一个问题。

    Tom answered 2020-02-02T13:48:48Z

    11 votes

    在Windows(cmd.exe),Linux(Bash和zsh)和OS X(zsh)上测试过的变通办法:

    #include

    void clrscr()

    {

    system("@cls||clear");

    }

    Jamesits answered 2020-02-02T13:49:33Z

    10 votes

    由于您提到cls,所以听起来您是在指Windows。 如果是这样,则此KB项具有将执行此操作的代码。 我只是尝试过,当我用以下代码调用它时,它就起作用了:

    cls( GetStdHandle( STD_OUTPUT_HANDLE ));

    Mark Wilkins answered 2020-02-02T13:49:53Z

    10 votes

    使用宏可以检查您是否在Windows,Linux,Mac或Unix上,并根据当前平台调用相应的函数。 如下所示:

    void clear(){

    #if defined(__linux__) || defined(__unix__) || defined(__APPLE__)

    system("clear");

    #endif

    #if defined(_WIN32) || defined(_WIN64)

    system("cls");

    #endif

    }

    nbro answered 2020-02-02T13:50:14Z

    8 votes

    #include

    和使用

    clrscr()

    Vivek Sharma answered 2020-02-02T13:50:33Z

    8 votes

    为了便于携带,请尝试以下操作:

    #ifdef _WIN32

    #include

    #else

    #include

    #define clrscr() printf("\e[1;1H\e[2J")

    #endif

    然后只需调用conio。在Windows上,它将使用printf的clrscr(),在Linux上,它将使用ANSI转义码。

    如果您真的想“适当地”执行此操作,则可以消除中间商(conio、printf等),而仅使用低级系统工具(准备进行大规模代码转储)即可:

    #ifdef _WIN32

    #define WIN32_LEAN_AND_MEAN

    #include

    void ClearScreen()

    {

    HANDLE hStdOut;

    CONSOLE_SCREEN_BUFFER_INFO csbi;

    DWORD count;

    DWORD cellCount;

    COORD homeCoords = { 0, 0 };

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

    if (hStdOut == INVALID_HANDLE_VALUE) return;

    /* Get the number of cells in the current buffer */

    if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;

    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    /* Fill the entire buffer with spaces */

    if (!FillConsoleOutputCharacter(

    hStdOut,

    (TCHAR) ' ',

    cellCount,

    homeCoords,

    &count

    )) return;

    /* Fill the entire buffer with the current colors and attributes */

    if (!FillConsoleOutputAttribute(

    hStdOut,

    csbi.wAttributes,

    cellCount,

    homeCoords,

    &count

    )) return;

    /* Move the cursor home */

    SetConsoleCursorPosition( hStdOut, homeCoords );

    }

    #else // !_WIN32

    #include

    #include

    void ClearScreen()

    {

    if (!cur_term)

    {

    int result;

    setupterm( NULL, STDOUT_FILENO, &result );

    if (result <= 0) return;

    }

    putp( tigetstr( "clear" ) );

    }

    #endif

    MD XF answered 2020-02-02T13:51:03Z

    5 votes

    没有C可移植的方式来执行此操作。 尽管像curses之类的各种光标操作库都是相对可移植的。 conio.h可在OS / 2 DOS和Windows之间移植,但不能移植到* nix变体。

    “控制台”的整个概念是超出标准C范围的概念。

    如果您正在寻找纯Win32 API解决方案,则Windows控制台API中没有单个调用可以执行此操作。 一种方法是填充足够多的字符的FillConsoleOutputCharacter。 或WriteConsoleOutput您可以使用GetConsoleScreenBufferInfo找出多少个字符就足够了。

    您还可以创建一个全新的控制台屏幕缓冲区,并使其成为当前的屏幕缓冲区。

    John Knoeller answered 2020-02-02T13:51:37Z

    2 votes

    视窗:

    system("clear");

    Unix:

    system("clear");

    您可以改为插入换行符,直到所有内容滚动为止,在这里看看。

    这样,您便可以轻松实现可移植性。

    gfe answered 2020-02-02T13:52:19Z

    1 votes

    只需输入clrscr(); 在void main()中起作用。

    例如:

    void main()

    {

    clrscr();

    printf("Hello m fresher in programming c.");

    getch();

    }

    clrscr();

    功能易于清除屏幕。

    Fridoon answered 2020-02-02T13:52:48Z

    0 votes

    正确的方法是使用tput或terminfo函数获取终端属性,然后根据尺寸插入换行符。

    Jack answered 2020-02-02T13:53:08Z

    -2 votes

    这应该工作。 然后只需调用cls(); 每当您想清除屏幕时。

    (使用之前建议的方法。)

    #include

    void cls()

    {

    int x;

    for ( x = 0; x < 10; x++ )

    {

    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

    }

    }

    JD3 answered 2020-02-02T13:53:33Z

  • 相关阅读:
    (附源码)计算机毕业设计SSM基于框架的人力资源管理系统
    [npm]脚手架本地全局安装1
    数仓主题域和数据域、雪花模型,星型模型和星座模型
    王道链表综合题(下)
    hadoop高可用集群配置
    如何使用csproj构建C#源代码组件NuGet包?
    【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
    UDDI初了解
    java学习集合二 Set集合 Map集合
    两道关于顺序表的经典算法
  • 原文地址:https://blog.csdn.net/u012294613/article/details/127687510