How to remove printable characters from command line in C ++

I downloaded the compiler (I think this is MinGW, but I'm not sure) on Windows 2000 the other day (I'm a Mac user, but it's not my machine), and the bootloader was MS-DOS, which displays a progress bar for loading. Something like that...

|---                 | 15%
...
|------              | 30%
...
...
|--------------      | 70%

except that it will be constantly updated on the same line. I assume that the program did this by deleting previously typed characters and retyping different ones, but I cannot figure out how to do this.

I tried typing the 'delete' character in several different ways, for example, (char)8and \b(even \rthat I heard to return to the beginning of the line in some languages), but none of these things worked.

Does anyone know how to do this?

: . , Mac.

+5
2

, , \b or \r, \b.

#include <iostream>
#include <iomanip>
#include <string>
#include <windows.h>

// This is the only non-portable part of this code.
// Simply pause for a specified number of milliseconds
// For Windows, we just call Sleep. For Linux, you'd
// probably call nanosleep instead (with a suitable
// multiplier, of course). Most other systems (presumably)
// have (at least vaguely) similar capabilities.
void pause(int ms) { 
    Sleep(ms);
}

static const int width = 40;    

void show_percent(int i) {
     int dashes = (width * i)/100;

     std::cout << '|' << std::left << std::setw(width) << std::string(dashes, '-') << '|' << std::setw(3) << i << "%";
}

int main() {

    for (int i=0; i<101; i++) {
        show_percent(i);
        std::cout << std::string(width+6, '\b');
        pause(100);
    }
}
+4

:

Win32 escape- ANSI. API-, ioctl, . escape- ANSI . [/P >

: http://msdn.microsoft.com/en-us/library/ms682073.aspx

, SetConsoleCursorPosition .

+2

All Articles