How to overwrite characters with printf?

Posted on 2007-10-07
Last Modified: 2012-06-21
I want to make printf to overwrite previously writen characters in the screen (stdout), like we see in many consele programs, I want to make an animated "Calculating......", or "Progress...."

The Idea is:

//Calculating.............. -> When there are 10 dots, then I erase all dots and start writing dot after dot again till it gets to 10.


Or that thing "Calculating \ | / -- \ | / --

How do I do that?
Question by:BUCHAS
    LVL 7

    Expert Comment

    You can do that with <conio.h> gotoxy() function.
    But it depends on which compiler do you use.
    Some of them don't have that header or function.
    LVL 13

    Accepted Solution

    The \b escape sequience will move the cursor back one, so the expression
    printf("\b") will move the cursor back one.

    For example
    #include <stdio.h>

    void main() {

    will display CalculatingQ.

    10 \b will move you back over the 10 dots.  You'll have to use something like "\n \b" to backspace over each dot, erasing the dot as you go.


    Assisted Solution

    Really sorry to say that I am not agreeing to josgood.After printing we can't do backspacing using '\b'.'\b' is used to do backspacing before printing.
    eg.printf("abc\bdef); will give the o/p abdef.

    As UrosVidojevic says we have to use gotoxy()  function to achieve this.I have written a sample code(not an optimized one) to illustrate this.

    #include <stdlib.h>

    void main()
          int processing = 1;
          int i = 0;
          int j = 0;
          char s[50];
          int tcount = 0;
          int x;
          int y;
                      printf ( "\r");
                x =  wherex();
                y =  wherey();            
                for(j = x;j>11;j--)
                      printf("%c",' ');            
                if(tcount == 2)
                      processing = 0;

    Hope this works.
    LVL 53

    Expert Comment

    >> As UrosVidojevic says we have to use gotoxy()  function to achieve this.

    gotoxy is not standard. And neither is the \b technique. So, I wouldn't say that you can't use one but have to use the other. It all depends what systems you want to use this for.

    Myself, I have found that most systems handle the \b correctly, and I use it from time to time with success. But never for code that is supposed to be portable.

    gotoxy is a VERY system specific solution (more so than \b), so my preference would also be the \b solution, assuming that you test whether it works on your system ;)
    LVL 13

    Expert Comment

    I didn't realize that \b is non-portable.  If Infinity08 says it isn't portable then it most likely isn't -- he's usually right.

    Infin, I stand corrected.  Have you a reference for which escape sequences are portable?

    On my system, WinXP with VS2005, \b works fine.  sjith2000's example of printf("abc\bdef"); displays as abdef.  The c is overwritten.
    LVL 2

    Expert Comment

    Maybe you could use '\r' (carriage return).
    In this case you have to print out the whole line every time.
    LVL 53

    Expert Comment

    >> Infin, I stand corrected.  Have you a reference for which escape sequences are portable?

    I might have caused some confusion.

    What I meant with portability, is that the code has to work on all systems, even the most exotic. The C standard does prescribe what \b should do, but not all systems can handle the backspace character correctly, for the simple reason that their display device can't do it (eg. it can't move back, or can't erase characters already written to the screen).

    A slight nuance maybe ;)

    For your reference - here's what the C standard says about it :

            \b (backspace)
            Moves the active position to the previous position on the current line. If the
            active position is at the initial position of a line, the behavior of the display
            device is unspecified.

    Btw, this also means that if you need to be able to move the cursor to a different line, you can't use \b.

    >> On my system, WinXP with VS2005, \b works fine.  sjith2000's example of printf("abc\bdef"); displays as abdef.  The c is overwritten.

    On most systems it works fine (as it should). It is however always best to test it out, instead of taking it for granted.

    The question asks to do something that is sometimes not trivially possible with standard C. So, any solution presented will have downsides. It's just a question which one you use. In my experience, gotoxy is less common, and so I prefer the \b solution.

    In short : your solution is good josgood - I didn't mean to make you think otherwise :) My response was more intended for sjith2000.
    LVL 13

    Expert Comment

    Thank you, Infin.
     I appreciate the clarification.
    LVL 2

    Author Closing Comment

    Thanks, good solutions guys. Sorry I have no more points to share with Infinity08 for the clarification.

    And sorry for the delay.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now