1

When I make a program where the progress is shown in the command line, how do I make 1 line which increases (or decreases) when the progress grows?

So that you don't get this:

Progress: 1%
Progress: 2%
Progress: 3%
Progress: 4%
Progress: 5%
Progress: 6%

But this:

Progress: PROGRESS%

So 1 single line for the whole operation

Can someone explain to me how this works?

tim687
  • 2,256
  • 2
  • 17
  • 28

4 Answers4

4

A common way is to write lines that DON'T end with a Newline (\n) character, but a Carriage Return (\r) instead, so they can be overwritten. For compatibility with the most platforms, one must manually flush the standard output after doing so, on pain of the line not being displayed.

Medinoc
  • 6,577
  • 20
  • 42
  • Would this work if you had say 5 lines of formatted text and want to overwrite them all? – Trevor Elliott Oct 09 '13 at 14:28
  • I'm afraid not, because you can't go back up one line with this method: You can only go the the start of the current line. For *that*, see Duncan Smith's answer. – Medinoc Oct 09 '13 at 14:31
1

In C# use Console.SetWindowPosition

In C++ (Windows, I presume) use SetConsoleCursorPosition.

(For non-Windows C++ use the ncurses library).

Duncan Smith
  • 530
  • 2
  • 10
0

You try with following

 Console.Write("\rProgress-{0}%   ", progvalue);
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
-1

You want to clear the console. In C# you can do this with Console.Clear:

http://msdn.microsoft.com/en-us/library/system.console.clear.aspx

In C++ I think you would do the following:

system("cls");

You should have a function which rewrites all the text to the console after each clear with the updated values.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • When I have a progress bar I certainly don't want my whole console to be cleared, because I need to see what happened before. -1 – syam Oct 09 '13 at 14:28
  • 1
    Not if you internally are storing the state and rewriting it each time. For complex ASCII charts or graphics like for example [this memtest console](http://www.memtest.org/pics/amd64-big.gif) I am pretty sure clearing the console is pretty standard. – Trevor Elliott Oct 09 '13 at 14:31
  • It's not much different than how in any Windows control you would store the state in variables and repaint the entire control every time on each WM_PAINT. – Trevor Elliott Oct 09 '13 at 14:32
  • Not saying you would do this if you just wanted to rewrite 1 line, but it's a valid approach for complex ASCII formatting. – Trevor Elliott Oct 09 '13 at 14:32
  • Yet OP explicitly asks for 1 line. – syam Oct 09 '13 at 14:37
  • 3
    You do realize that on StackOverflow answers are not just for the OP? They are for everyone to find in years to come with similar search terms. An asker might search for "Console.Clear" looking for alternatives and find "Console.SetWindowPosition" in this thread, or vice-versa could happen. It is best to answer with all solutions. – Trevor Elliott Oct 09 '13 at 14:42
  • How can I make it clear to you? In a console, you run a bunch of different programs, bound together usually with a shell script. Then comes your program that needs to display a progress bar. How is it going to keep the previous programs' output which you know nothing about if you're clearing the screen? Sorry if I am a pain, but this does *not* answer OP's question. – syam Oct 09 '13 at 14:52
  • You do realize that on StackOverflow answers are not just for the OP? – Trevor Elliott Oct 09 '13 at 14:53
  • Oh gosh. You *do* realize that on SO answers are meant to answer OP's question, right? No need to answer me, that's just a rhetorical question. I certainly won't answer back anyway. – syam Oct 09 '13 at 14:55