How do I clear the console in BOTH Windows and Lin

2019-01-07 09:49发布

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 the end-user programmer to have to change any code in my program to get it to clear for Windows vs Linux (for example if it has to pick between two functions then the decision has to be made at run-time or at compile-time autonomously).

12条回答
别忘想泡老子
2楼-- · 2019-01-07 10:39

On linux it's possible to clear the console. The finest way is to write the following escape sequence to stdout:

write(1,"\E[H\E[2J",7);

which is what /usr/bin/clear does, without the overhead of creating another process.

查看更多
三岁会撩人
3楼-- · 2019-01-07 10:45

Short answer: you can't.

Longer answer: Use a curses library (ncurses on Unix, pdcurses on Windows). NCurses should be available through your package manager, and both ncurses and pdcurses have the exact same interface (pdcurses can also create windows independently from the console that behave like console windows).

Most difficult answer: Use #ifdef _WIN32 and stuff like that to make your code act differently on different operating systems.

查看更多
Fickle 薄情
4楼-- · 2019-01-07 10:48

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
}
查看更多
一纸荒年 Trace。
5楼-- · 2019-01-07 10:49

A simple trick: Why not checking the OS type by using macros in combination with using the system() command for clearing the console? This way, you are going to execute a system command with the appropriate console command as parameter.

#ifdef _WIN32
#define CLEAR "cls"
#else //In any other OS
#define CLEAR "clear"
#endif

//And in the point you want to clear the screen:
//....
system(CLEAR);
//....
查看更多
虎瘦雄心在
6楼-- · 2019-01-07 10:49

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__ */
查看更多
老娘就宠你
7楼-- · 2019-01-07 10:55

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
}
查看更多
登录 后发表回答