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

匆匆过客 提交于 2019-12-18 11:39:33

问题


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 to clean cpmpletely the screen, but I want to open a "new page", such as in the NANO and VI editors. Thanks


回答1:


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




回答2:


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);
} 



回答3:


#include <stdlib.h>
int main(void)
{
    system("clear");
}



回答4:


Portable UNIX code should be using the terminfo database for all cursor and screen manipulation. This is what libraries like curses uses to achieve its effects like windowing and so forth.

The terminfo database maintains a list of capabailities (like clear which is what you would use to clear the screen and send the cursor to the top). It maintains such capabilities for a wide range of devices so that you don't have to worry about whether you're using a Linux console or a (very dated) VT52 terminal.

As to how you get the character streams for certain operations, you can choose the time-honored but rather horrible method of just using system to do it:

system ("tput clear");

Or you can capture the output of that command to a buffer so later use involve only outputting the characters rather than re-running the command:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static char scrTxtCls[20]; static size_t scrSzCls;

// Do this once.

FILE *fp = popen ("tput clear", "r");
scrSzCls = fread (scrTxtCls, 1, sizeof(scrTxtCls), fp);
pclose (fp);
if (scrSzCls == sizeof(scrTxtCls)) {
    actIntelligently ("you may want to increase buffer size");
}

// Do this whenever you want to clear the screen.

write (1, cls, clssz);

Or, you can link with ncurses and use its API to get whatever capabilities you want, though this might drag in quite a bit of stuff for something as simple as clearing the screen. Still, it's an option to be considered seriously since it gives you a lot more flexibility.




回答5:


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)




回答6:


You can achieve this using CSI sequences:

#include <stdio.h>
int main()
{
    printf("\x1b[H\x1b[J");
}

What does \x1b[H?

Actually it is the same as \x1b[1;1;H, it means that it will move the cursor to row 1 and column 1.

What does \x1b[J a.k.a \x1b[0;J?

If n is 0 or missing, it will clear from cursor to end of screen.

Source: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences




回答7:


To clear the screen using termcaps, use this :

write(1, tgetstr("cl", 0), strlen(tgetstr("cl", 0)));



回答8:


Just use #include<stdlib.h> after #include<stdio.h>.

Then you can use the command system("clear");after main() {

i.e:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    system("clear");

After these commands you can continue with your program.

Hope this helps :)




回答9:


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




回答10:


This code is for clear screen with reset scrollbar position in terminal style windows #include <iostream> int main(){ std::cout << "\033c"; return 0;}



来源:https://stackoverflow.com/questions/17271576/clear-screen-in-c-and-c-on-unix-based-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!