Thread: keeping the printf() in the same place on the screen

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    keeping the printf() in the same place on the screen

    I am new to C programming. I have looked through the tutorials and have not been able to find this.

    I have not been able to find a way to process a printf statement and keep it it the same place on the screen.

    I have a calculation that repeats its self in a loop until it has completed its loop.
    ie; count from 1 to 100

    I want to display printf("The current count is:",variable for count);

    I want to display the calculation as it loops to stay in the same spot on the screen instead of printing a list that goes down the monitor.

    Is there any way I can do this?

    Jack

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You could add an instruction to clear the screen after each print I guess. Not really sure you would actually see anything though. Why would you want to do that anyway?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I was able to do something like this before, but I can't remember exactly how.

    I believe I either printed a carriage return without a linefeed before the text each time I printed it:
    Code:
    while ( ... )
    {
       ...
       printf("\rCount: %3d", countVar++);
    }
    or I printed backspaces:
    Code:
    printf("Count: ");
    
    while ( ... )
    {
       ...
       printf("%3d\b\b\b", countVar++);
    }
    Whatever it was, I'm sure it's not portable at all. If you have a non-standard console library available (conio.h for Windows as an example), you can use caret positioning to achieve the same thing.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are 3 ways to do it. One uses the old conio.h include file header, or ncurses library.

    Second way is to use the Windows API SetConconsoleCursorPosition, an example of which is in our FAQ under "How do I clear the screen", advanced.

    This is the all C way, using the escape char \b and printf():

    Code:
    #include <stdio.h>
    #include <dos.h>      //just for the delay() function
    
    
    int main() {
      int i, n ; 
      printf("\n\n The Number is: ");
      for(i = 50; i > -1; i--)  {
        printf("%2d", i);
        delay(400);
        printf("\b\b");
      }
    
    
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar();
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Thanks

    Thank for the help!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    ANSI escape sequences are portable and should work on all terminals.
    Code:
    printf("\033[2J");        /*  clear the screen  */
    printf("\033[H");         /*  position cursor at top-left corner */
    
    for (i=1; i<=100; i++) ) {
        printf("The current count is: %d", i);
        fflush(stdout);
        sleep(1);
        printf(i < 100 ? "\033[H" : "\n");
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by itCbitC View Post
    ANSI escape sequences are portable and should work on all terminals.
    Doesn't seem to work on Windows7, though.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ANSI escape sequences are only going to work if your system uses the ansi.sys driver.

    I have WinXP and ANSI escape sequences will not work on this system.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Adak View Post
    ANSI escape sequences are only going to work if your system uses the ansi.sys driver.

    I have WinXP and ANSI escape sequences will not work on this system.
    Ah, okay. Damn Windows.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Lightbulb "\b" is for backspace

    There is also the easy way:
    Code:
    #include <stdio.h>
    
    int main() {
    	int i = 0;
    
    	printf("Count is: %2d", i);
    	for (;i<60;i++) {
    		printf("\b\b%2d",i); fflush(stdout);
    		sleep(1);
    	}
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by MK27 View Post
    There is also the easy way:
    Code:
    #include <stdio.h>
    
    int main() {
        int i = 0;
    
        printf("Count is: %2d", i);
        for (;i<60;i++) {
            printf("\b\b%2d",i); fflush(stdout);
            sleep(1);
        }
    
        return 0;
    }
    I guess. It is a bit of a kludge, though.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sebastiani View Post
    I guess. It is a bit of a kludge, though.
    Yes, better to use ANSI sequences or import an entire console library What have YOU been smoking???
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by MK27 View Post
    What have YOU been smoking???
    I won't say, but I can assure you that I haven't smoked enough of it...yet (it's still early).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  3. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  4. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  5. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM