Thread: Easy One!!

  1. #1
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66

    Easy One!!

    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.
    "take the long road.... and walk it."

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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;
    }
    My best code is written with the delete key.

  3. #3
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    nice one.

    somebody buy that man a beer.
    "take the long road.... and walk it."

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by dug
    nice one.

    somebody buy that man a beer.
    Uh oh..
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by MrWizard
    Uh oh..
    Yeah. Duck and Cover!!!

    You can also use \r to return to the beginning of the line.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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%
    '\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).

    >Uh oh..
    >Yeah. Duck and Cover!!!
    My best code is written with the delete key.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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%
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    a thousand pardon's prelude...

    and thanks for your help.
    "take the long road.... and walk it."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM