Displaying progress dots in ANSI C++

A

ambros.gleixner

Hello everyone,

I was wondering if (in pure command line, "non-visual" C++) there is a
simple way of regularly calling an output function in the background
while a process, e.g. loading of a huge file, is running. I am thinking
of displaying a classical

Loading.....

output where every second a new dot is displayed, for instance, maybe
starting anew after 10 dots or something.

I hope this is the appropriate group to post to...thanks for your help,
ambros
 
V

Victor Bazarov

I was wondering if (in pure command line, "non-visual" C++) there is a
simple way of regularly calling an output function in the background
while a process, e.g. loading of a huge file, is running. I am
thinking of displaying a classical

Loading.....

output where every second a new dot is displayed, for instance, maybe
starting anew after 10 dots or something.

I hope this is the appropriate group to post to...thanks for your
help, ambros

int iteration = 0;
... // long process start
if (figure_out_that_another_second_passed)
{
int ndots = ++iteration % 10;
std::cout << "\rLoading" << std::string(ndots, '.');
std::cout.flush();
}

There is no other way. If your OS supports threading, you could try
doing output in another thread and synchronize them somehow, but C++
language doesn't define any threading, you need to ask in another NG,
line one that's dedicated to your OS.

V
 
C

carlmuller

Hello everyone,

I was wondering if (in pure command line, "non-visual" C++) there is a
simple way of regularly calling an output function in the background
while a process, e.g. loading of a huge file, is running. I am thinking
of displaying a classical

Loading.....

output where every second a new dot is displayed, for instance, maybe
starting anew after 10 dots or something.

I hope this is the appropriate group to post to...thanks for your help,
ambros

The traditional way is to put a callback function in the lengthy
function that voluntarily calls the callback every so often. For
example you can print a spinning line / - \ | followed by backspace
each time. Otherwise you have to descend into threads, and lose the
useful property of knowing that the code has not crashed.
 
G

Gregg N

I was wondering if (in pure command line, "non-visual" C++) there is a
simple way of regularly calling an output function in the background while
a process, e.g. loading of a huge file, is running. I am thinking of
displaying a classical

Loading.....

output where every second a new dot is displayed, for instance, maybe
starting anew after 10 dots or something.

I suggest instead of making it time-based, making it progress-based. For
example,

bool incremental_load()
{
// Load logical chunk of data from large file
// Return false if this is the last chunk, false if
// more data left.
}

void update_spinner()
{
// Can be as fancy as you want. Here, just print dots.
std::cout << ".";
}

void main_loop()
{
while (incremental_load())
update_spinner();
}

Gregg
 
A

ambros.gleixner

Thank you all for your quick comments! Sorry that I didn't repsond
immediately as I was held up the last days unexpectedly.
For what I'm trying to do it looks like I would have to start using
threads, and I am afraid that this wouldn't be worth the hassle for
just some fancy output. If I understood it correctly, it would also
make my programme platform dependent, which is a good thing to avoid. I
think I'll continue with constant dots. Thanks a lot for your advice,

ambrose
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top