Skip to content
Advertisement

Clearing output of a terminal program in Linux C

I want to clear the output of a C program produced with printf statements. I want to clear only one line, for example:

[source]

JavaScript

[terminal]

JavaScript

[I hope]

JavaScript

I will “DDDDDDDDDDDDDD” line in write other string. I just want the above A, B, C sentences to left. Only clear D sentences to change the other sentences, unconditionally output D sentences.

How do I do this?

Advertisement

Answer

There’re several ways to delete the DDDDDDDDDDDDDD

  1. Print backspace several times
JavaScript
  1. Print carriage-return and then print something to override the original line
JavaScript
  1. If you are in a newline. You may use terminal commands to move your cursor

such as printf("33[8;5Hhello"); // Move to (8, 5) and output hello

Other commands:

JavaScript
  1. Don’t forget ncurses

It is the best ways to control the exact layout and format in a terminal

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement