17

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

10 Answers10

24

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

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
16

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);
} 
Shar
  • 455
  • 2
  • 14
6

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
6
#include <stdlib.h>
int main(void)
{
    system("clear");
}
Naffi
  • 710
  • 1
  • 6
  • 13
3

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)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

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

Edgar Luque
  • 388
  • 1
  • 4
  • 14
2

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 :)

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
1

To clear the screen using termcaps, use this :

write(1, tgetstr("cl", 0), strlen(tgetstr("cl", 0)));
Jonsmoke
  • 822
  • 9
  • 18
1

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

0

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

#include <iostream>

int main(){
   std::cout << "\033c";
   return 0;
}
Shipof123
  • 233
  • 2
  • 11
Andreadjk
  • 1
  • 1