Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing terminal in Linux with C++ code

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 program so that I can keep my program running?

I know I can just type clear in terminal and it clears it fine, but like I said, for this program it would be more beneficial for me.

I found something that works, however, I'm not sure what it is or what it is doing.

cout << "\033[2J\033[1;1H"; 

That works but I have no clue what it is, if you could explain it, than I would much appreciate it.

like image 522
John Avatar asked Oct 31 '10 06:10

John


People also ask

How do you clear the screen in C or code?

To clear the screen in Visual C++, utilize the code: system("CLS"); The standard library header file <stdlib. h> is needed.

What is cls in C?

“cls” Means , clear screen. Every time this piece of code is processed whatever is written in your screen will get cleared and the remaining portion of code's output if any will be displayed.

How do you clear an entire terminal?

A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.

How do I clear the console in Linux C++?

For clearing the console in linux, we can use “clear” command. This will be passed inside the system() function.


1 Answers

These are ANSI escape codes. The first one (\033[2J) clears the entire screen (J) from top to bottom (2). The second code (\033[1;1H) positions the cursor at row 1, column 1.

All ANSI escapes begin with the sequence ESC[, have zero or more parameters delimited by ;, and end with a command letter (J and H in your case). \033 is the C-style octal sequence for the escape character.

See here for the full roadshow.

like image 142
Marcelo Cantos Avatar answered Oct 06 '22 22:10

Marcelo Cantos