Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete printed characters from command line in C++

I was downloading a compiler (I think it was MinGW but I'm not sure) on windows 2000 the other day (I'm generally a Mac user, but it wasn't my machine), and the downloader was a MS-DOS command line app that would display a progress bar for the download. Something like this...

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

except that it would continuously update on the same line. I assume the program accomplished this by deleting previously printed characters and reprinting different ones, but I can't seem to figure out how to do this.

I've tried to print a 'delete' character several different ways, like (char)8 and \b (even \r, which I heard backtracks to the beginning of the line in some languages), but none of those things worked.

Does anyone know how to do this sort of stuff?

Edit: This question has become platform-specific. I want to know specifically how to accomplish this on a Mac.

like image 849
Michael Dorst Avatar asked Jun 30 '12 04:06

Michael Dorst


People also ask

How do I delete a character in CMD?

# Deleting whole words ALT+Del Delete the word before (to the left of) the cursor ALT+d / ESC+d Delete the word after (to the right of) the cursor CTRL+w Cut the word before the cursor to the clipboard # Deleting parts of the line CTRL+k Cut the line after the cursor to the clipboard CTRL+u Cut/delete the line before ...

How do you delete all messages in CMD?

del /s *. txt will delete all TXT files in all subfolders of current working directory.

How do you clear the console line in C++?

To clear the screen in Visual C++, utilize the code: system("CLS"); The standard library header file <stdlib. h> is needed. Note: If you wish to clear the screen after a cout statement, you will need to "flush" the iostream.


2 Answers

I'm not sure why you ran into problems, but either \b or \r can be used to do this, I have used \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);
    }
}
like image 57
Jerry Coffin Avatar answered Oct 09 '22 22:10

Jerry Coffin


According to Wikipedia:

The Win32 console does not support ANSI escape sequences at all. Software can manipulate the console with the ioctl-like Console API interlaced with the text output. Some software internally interprets ANSI escape sequences in text being printing and translates them to these calls [citation needed].

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

I believe SetConsoleCursorPosition is what allows you to replace text.

like image 44
Pubby Avatar answered Oct 09 '22 21:10

Pubby