Clearing output of a terminal program Linux C/C++

47,456

Solution 1

Most terminals support ANSI escape codes. You can use a J (with parameter 2) to clear the screen and an H (with parameters 1,1) to reset the cursor to the top-left:

printf("\033[2J\033[1;1H");

Alternatively, a more portable solution would be to use a library such as ncurses, which abstracts away the terminal-specific details.

Solution 2

Once you print something to the terminal you can't easily remove it. You can clear the screen but exactly how to do that depends on the terminal type, and clearing the screen will remove all of the text on the screen not just what you printed.

If you really want fine control over the screen output use a library like ncurses.

Solution 3

You can also try something like this, which clears the entire screen:

printf("\033[2J\033[1;1H");

You can include \033[1;1H to make sure if \033[2J does not move the cursor in the upper left corner.

More specifically:

  • 033 is the octal of ESC
  • 2J is for clearing the entire console/terminal screen (and moves cursor to upper left on DOS ANSI.SYS)
  • 1;1H moves the cursor to row 1 and column 1

Solution 4

As far as C is concerned, stdout is nothing more than a byte stream. That stream could be attached to a CRT (or flat screen), or it could be attached to a hardcopy device like a teletype or even a sheet-fed printer. Calling rewind on the stream won't necessarily be reflected on the output device, because it may not make any sense in context of that device; think about what rewinding would mean on a hardcopy terminal or a sheet-fed printer.

C does not offer any built-in support for display management, so you'll have to use a third-party library like ncurses.

Solution 5

In fact, when you capture/redirect your stdout (./program > output.file), there is no way how to remove contents of that file, even printf("\033[2J\033[1;1H"); just adds this sequence of characters into it.

Share:
47,456
ldog
Author by

ldog

Favorite exercise/sport: mountain biking, Favorite software: vim, Favorite board game: chess, Favorite academic topic: math, Favorite movie: big lebowski Favorite programming language: C++, Favorite OS: linux, Favorite bike: Specialized Trail SX, Favorite topic in programming: algorithms

Updated on November 13, 2020

Comments

  • ldog
    ldog over 3 years

    I'm interested in clearing the output of a C program produced with printf statements, multiple lines long.

    My initial guess was to use

     printf("output1\n");
     printf("output2\n");
     rewind(stdout);
     printf("output3\n");
     printf("output4\n");
    

    but this produces

     output1
     output2
     output3
     output4
    

    I was hoping it would produce

     output3
     output4
    

    Does anyone know how to get the latter result?

  • LeoNerd
    LeoNerd about 12 years
    The parameters 1;1 are entirely optional with CSI H. It defaults to the home position (1,1) anyway. You can simply \033[H to home the cursor (I use the mnemonic "home" to remember it's H)