How do I clear the console in BOTH Windows and Linux using C++

前端 未结 12 1476
我寻月下人不归
我寻月下人不归 2020-11-30 01:54

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

相关标签:
12条回答
  • 2020-11-30 02:22

    Short answer

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

    Long answer, please read:

    system("pause") clarification

    0 讨论(0)
  • 2020-11-30 02:23

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

    // 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__ */
    
    0 讨论(0)
  • 2020-11-30 02:24

    I know this isn't answering my own question but! This works for Windows (#include <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);
    }
    
    0 讨论(0)
  • 2020-11-30 02:24

    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 (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.

    0 讨论(0)
  • 2020-11-30 02:29

    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
    }
    
    0 讨论(0)
  • 2020-11-30 02:30

    This is how you do it on any other platform but it doesn't work in 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
    }
    
    0 讨论(0)
提交回复
热议问题