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.
# 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 ...
del /s *. txt will delete all TXT files in all subfolders of current working directory.
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.
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With