How to 'edit' existing command prompt lines

Have any of you ever seen an installation from the command prompt?

I will use MinGW installing g++ as an example:

When it's downloading from sourceforge, it has a little progress bar made of equals:

[==============] (or something like that)

When this bar updates itself as the packages are downloading, it doesn't change lines: it updates the bar on the same line as before so you don't have a bunch of leftover lines.

Is there anyway I can replicate this process?

If I'm being too vague, let me know.

Thanks.
Last edited on
If I understand you right you're wanting kind of a progress bar made up of equal signs or whatever that adds the symbol to the line as processing proceeds.

Depending on the effect you want, it could get a little dicey with just the C Std. Lib. functions, but with the Console Functions from the Windows Api it is doable. Look up SetConsoleCursorPosition. Its difficult to use, but hey, what isn't?
So you're saying I would have to use OS-specific libs to make this work?

I would like to stay away from os libraries.

And not just add characters, change the existing values on the characters as well.

Here's exactally what I'm doing:

I have a array of characters like this:

$$$
$$$
$$$

And I want to update this like so:

$@$
$$$
$$$

and another frame:

$$$
$@$
$$$

Without re-printing the same characters.

Please help.
The normal way to do it is to position the cursor at the begin of the line, delete everything on this line and output the new stuff.
I would like to stay away from os libraries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h>
#include <string>


int main()
{
    std::string progress;
    
    for (int i=0; i<40; i++)
    {
        progress += '*';
        std::cout << progress << '\r' << std::flush;
        Sleep(250);
    }
    
}

Here I've used the windows header only for the Sleep() function, which is incidental. However, I don't think this code would behave the same on other platforms.

For a platform-independent solution then ncurses may be what you are looking for.
@Chervil

Why use the sleep function?

What if I wanted to update every time a action occured (like a command or something)?

BTW thanks for help.

Also, what do '\r' and the flush function do?

EDIT: I see why u use sleep (to create visible delay). I won't need to do that. Thanks.
Last edited on
Yes, Sleep() was just to simulate some other lengthy process.

what do '\r' and the flush function do?
'\r' Is carriage return. In this case it moves the cursor back to the start of the current line.
(See also '\n' which moves the cursor down to the next line).
flush may not be necessary - it was a precaution depending on the environment, sometimes the output is not displayed immediately. The output is first stored in a buffer. Flush simply forces the buffer to be emptied and its contents sent to the physical device - in this case the console.

What if I wanted to update every time a action occurred (like a command or something)?

You'd probably want to call a function each time. The function will update the console display and then return to continue whatever else the program was doing.
Topic archived. No new replies allowed.