Clearing terminal in Linux with C++ code

后端 未结 3 1108
粉色の甜心
粉色の甜心 2020-11-28 05:31

Okay, I have been researching on how to do this, but say I am running a program that has a whole bit of output on the terminal, how would I clear the screen from within my p

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 06:21

    Instead of depending on specific escape sequences that may break in unexpected situations (though accepting that trade-off is fine, if it's what you want), you can just do the same thing you'd do at your shell:

    std::system("clear");
    

    Though generally system() is to be avoided, for a user-interactive program neither the extra shell parsing nor process overhead is significant. There's no problem with shell escaping either, in this case.

    You could always fork/exec to call clear if you did want to avoid system(). If you're already using [n]curses or another terminal library, use that.

提交回复
热议问题