Skip to main content
GameDev.net gamedev.net
🔒 Locked

Quick and Easy printf() question

Started by 31337 Feb 3, 2004 at 11:57 PM 9 replies 400+ views
Original Post
31337
31337
How can I get printf to erase the last character? I tried writing a program in redhat that did the following but it wouldn''t work. I wanted it to print out: -\/ giving it the effect of a twirling little batone (if you will). But for some reason it just stayed on one character forever. I even tried adding in a sleep sequence to see if that was the problem. No can do! Does anyone have any suggestions or ideas on how I can achieve this simple console effect?
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
RenderTarget
RenderTarget
man curses

I like pie.
[sub]My spoon is too big.[/sub]
Oluseyi
Oluseyi
You can''t do it with printf (the effect isn''t as "simple" as you think, btw). For Unix/Linux, look for [n]curses.
Paradigm Shifter
Paradigm Shifter
void twiddle()
{
static int index = 0;
const char twiddles[] = "|/-\";

printf( "%c\b", twiddles[index++] );
index %= sizeof( twiddles ) - 1;
}

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Oluseyi
Oluseyi
@Paradigm Shifter:
Your program will eventually fill the screen with a looping sequence of characters, which is quite different from the OP''s desired effect of animating the sequence of characters in-place.
Paradigm Shifter
Paradigm Shifter
quote:
Original post by Oluseyi
@Paradigm Shifter:
Your program will eventually fill the screen with a looping sequence of characters, which is quite different from the OP''s desired effect of animating the sequence of characters in-place.


Not if \b is the escape code for a backspace it won''t.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
cgoat
cgoat
printf("%c\b");

Umm, that erases the character right after you print it, which effectively does nothing.

You want printf("\b%c"); to erase the previous char and put the new one in its place. Don't forget to initialize the loop by writing the first character without the backspace though.

Not to mention buffering also, which means you should call fflush(stdout); after each print.

I tried it on Redhat 6.2, and it works ok, except you need to come up with a way to interrupt the loop when a key is pressed. Don't forget a delay between prints also.

I wouldn't suggest it as a portable solution though. If it works on your machine, fine, but don't expect it to work in all situations.

[edited by - cgoat on February 4, 2004 12:54:33 PM]
Oluseyi
Oluseyi
quote:
Original post by Paradigm Shifter
Not if \b is the escape code for a backspace it won''t.
You''re right, but it''s not portable. I forgot the OP wanted a Red Hat solution, which yours is. On Windows, you get a non-printing character for \b.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.