   Quick and Easy printf() question
  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? 
  man curses   I like pie. ;  You can''t do it with printf (the effect isn''t as "simple" as you think, btw). For Unix/Linux, look for [n]curses. ;  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 ;  @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. ;  void twiddle(){static int index = 0;const char twiddles[] = "|/-\";printf( "%c\b\n%c[{1}A", twiddles[index++], 27 );index %= sizeof( twiddles ) - 1;} Try that, i''m assuming its a VT100 or compatible terminal where ansi control codes will work.  Also I just altered the previous guys code so I dont know if it worked in itself - I never used ''%='' before.  Also he didn''t say that ud have to call the function in a loop with a delay between each call.;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. ;  oops forgot about \b ;  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];  also \r might be helpful, to go back to the beginning of the line. 