Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i write over the last line in the console?

I want to show a progress bar (like wget) how do i keep writing to the last line in the console?

Windows 7 vis 2005 c++

like image 586
Lodle Avatar asked Jun 23 '09 09:06

Lodle


People also ask

How do you overwrite a line in console?

How to overwrite an existing line without terminal sequence. The \r or carriage return will put the cursor at the beginning of the line and allows you to overwrite the content of the line.

What are the two ways for writing in console?

Writing in Console AppWriteLine() or Console. Write(), basically both methods are used to print output of console.

How to insert new line in Console WriteLine?

By using: \n – It prints new line. By using: \x0A or \xA (ASCII literal of \n) – It prints new line. By using: Console. WriteLine() – It prints new line.

What is Console WriteLine()?

WriteLine(Object) Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream. WriteLine(String, Object, Object, Object, Object)


2 Answers

with carriage-return ("\r") you can jump back to the beginning of the current line. This will only work for terminals which have support for this feature.

After you jumped back you can just print your new status-line.

like image 112
Philipp Avatar answered Oct 21 '22 12:10

Philipp


If you need to go back further than the last line and are using native Win API, you can use the SetConsoleCursorPosition method in kernel32.lib.

Edit: GnuWin32 also seems to have a ncurses port if you can live with the dependencies on GnuWin32.

like image 7
Ben Schwehn Avatar answered Oct 21 '22 13:10

Ben Schwehn