C++ Console Progress Indicator
c++, console, linux
Solution
A very simple way to do it is to print out a string followed by a `'\r'` character. That is carriage return by itself and on most consoles, it returns the cursor to the beginning of the line without moving down. That allows you to overwrite the current line.
If you are writing to stdout or cout or clog remember to fflush or std::flush the stream to make it output the line immediately. If you are writing to stderr or cerr then the stream is unbuffered and all output is immediate (and inefficient).
A more complicated way to do it is to get into using a screen drawing library like curses. The Windows consoles have some other ways of setting them for direct screen writing but I don't know what they are.
Problem
What would be an easy way of implementing a console-based progress indicator for a task that's being executed, but I can't anticipate how much time it would take? I used to do this back when I coded in Clipper, and it was only a matter of iterating through the chars '/', '-', '\', '|' and positioning them in the same place. Any way / links / libs for doing that (or something similar) in C++? The target for this is *nix environments. Edits: - changed the title to be more coherent and generic; - added target environment.