Clear screen in C and C++ on UNIX-based system?

前端 未结 10 1559
青春惊慌失措
青春惊慌失措 2020-12-08 08:23

I want to know: how to clean screen on an UNIX-based system? I searched on the Internet, but I\'ve just found how to do it on Windows: system(\"CLS\") I don\'t want exactly

相关标签:
10条回答
  • 2020-12-08 08:47

    It is usually not a matter of just clearing the screen, but of making a terminal aware application.

    You should use the ncurses library and read the NCURSES programming HowTo

    (You could perhaps use some ANSI escape codes as David RF answered, but I don't think it is a good idea)

    0 讨论(0)
  • 2020-12-08 08:47

    This code is for clear screen with reset scrollbar position in terminal style windows

    #include <iostream>
    
    int main(){
       std::cout << "\033c";
       return 0;
    }
    
    0 讨论(0)
  • 2020-12-08 08:56

    You can use the following code which use termcap for clear screen. (don't forget to link with the library)

    #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);
    } 
    
    0 讨论(0)
  • 2020-12-08 09:00

    Maybe you can make use of escape codes

    #include <stdio.h>
    
    #define clear() printf("\033[H\033[J")
    
    int main(void)
    {
        clear();
        return 0;
    }
    

    But keep in mind that this method is not compatible with all terminals

    0 讨论(0)
  • 2020-12-08 09:02

    Use system("clear"); with header #include <stdlib.h> (for C Language) or #include <cstdlib> (for C++).

    0 讨论(0)
  • 2020-12-08 09:04
    #include <stdlib.h>
    int main(void)
    {
        system("clear");
    }
    
    0 讨论(0)
提交回复
热议问题