how do you print to the same position/line in stdout?
so for instance if you wanted to do a progress meter for the command prompt... how would you update the %age completed at the command prompt?
can't figure it out... thanks in advance.
Printable View
how do you print to the same position/line in stdout?
so for instance if you wanted to do a progress meter for the command prompt... how would you update the %age completed at the command prompt?
can't figure it out... thanks in advance.
Use the '\b' character to go back:
Code:#include <stdio.h>
#include <windows.h> /* For Sleep */
int main ( void )
{
int progress = 0;
while ( progress <= 100 ) {
printf ( "\b\b\b\b%3d%%", progress++ );
fflush ( stdout );
Sleep ( 100 );
}
printf ( "\n" );
return 0;
}
nice one. ;)
somebody buy that man a beer.
Uh oh.. :eek:Quote:
Originally posted by dug
nice one. ;)
somebody buy that man a beer.
Yeah. Duck and Cover!!! :pQuote:
Originally posted by MrWizard
Uh oh.. :eek:
You can also use \r to return to the beginning of the line.
>You can also use \r to return to the beginning of the line.
That isn't as flexible though. Most of the time you would prefer to have a format such as this:
'\r' doesn't give you the ability to do that unless you want to continually rewrite the part of the message that remains constant. This would be a waste and may result in flashiness (the bad kind).Code:Percent complete: 10%
>Uh oh.. :eek:
>Yeah. Duck and Cover!!! :p
:rolleyes:
dug, in case you're wondering what all the stupid responses are about, prelude is a woman.
But someone can buy that woman a beer if her husband doesn't mind.
No, it's not as flexible, but it definitely has its uses. For example a progress bar instead of a percentage. It's much harder with \b whereas \r would be much easier.Quote:
Originally posted by Prelude
>You can also use \r to return to the beginning of the line.
That isn't as flexible though. Most of the time you would prefer to have a format such as this:
Code:Percent complete: 10%
a thousand pardon's prelude...
and thanks for your help.