Thread: refreshing line?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    refreshing line?

    I have no clue if this is even possible using C, but it was worth asking. What I want to do is have a number refresh itself ( think of a countdown and the number keeps changing ). A perfect example is if you have two operating systems on your pc, and when you boot if will say select which one you want to boot and if gives you like 15 seconds or w/e to choose. I just want to print a line and have a number in it and have the number countdown or count up or w/e I want the number to do. Does this make since to anyone? If anyone knows if this is possible or how to do it please help!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Use the backspace character \b. For example,
    Code:
    #include <stdio.h>
    
    int main(void) {
      unsigned int i, j;
      for (i=0; i < 1000000000; ++i) {
        j = printf("&#37;u", i);
        while (j--) printf("\b");
      }
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    ok what does j = if it is = to printf().. that is new to me? I know you could make something equal to a function that returns a number or something... is that what this is doing?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, printf returns the number of chars that it printed.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    oh really.. hah good to know... so for example if I did this

    Code:
    int i;
    i = printf("Bob");
    That would make i equal to 3????
    or four because of the nulll???

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try it. Three. printf() doesn't actually print the null char.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robatino View Post
    Use the backspace character \b.
    Another method is to use the carriage return character '\r'. This moves the cursor back to the beginning of the line. This doesn't CLEAR the line, however, so you need to write a sufficient number of spaces in order to "blank out" anything that was already there.

    Neither character is guaranteed to cause a buffer flush, so if you want to see the results immediately, be sure to fflush(stdout) after printing your data.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    oh ya don't numbers not have a null or something( I am fairly new to C.... ha). But lets say I was trying to do the same concept only with a 3 letter word, would I have to do that same thing only like put the printf("\b"); four times.. such as this.... ???

    printf("\b\b\b\b");

  9. #9
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    could someone also explain to me what the fflush() does exactly and why the buffer needs to be flushed?... I hate to be just a pain but I need to learn somehow...

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    FAQ will answer your question.

    ssharish2005

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you attempt to read the manual page for fflush() in the 4 minutes it took for you to reply?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    ya.. I actually did, and it made no sense to me... so don't get all ........y... it isn't like I asked you specifically to help me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  2. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM