Thread: Erase a line in console

  1. #1
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    Erase a line in console

    Ok, so i'm making a program where I have to update the user constantly, but I don't want the output to go over the console buffer space, so I thought if I printed something,
    Code:
    printf("%s", result);
    I need to erase it, and print the next thing.
    I tried
    Code:
    printf("%s", result);
    for (i = 0; i == strlen(result); i++)
    {
        printf("\b");
    }
    But that didn't work properly.
    There were still characters there when I printed the next bit of information.
    Any idea's?
    Thanks.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I doubt that what you are trying to do could be easily done without some major hacking around and whatnot. What you could do is before you print data to the console, add it to an ever growing buffer.

    Then when you wanted to delete the previous line, you could clear the console screen, remove your last entry into the buffer, and printf the contents of your buffer.

    If you were using C, I would recommend a linked list, since you could easily remove your previous node. Since this is posted in the C++ forum, maybe someone else would have a better idea.

    By the way, your escape sequence isn't going to work like you want it to. I believe what it would do is something like this:
    Code:
    printf("Hello World!\b?");
    Output: Hello World?
    Last edited by carrotcake1029; 07-17-2008 at 11:18 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bradszy View Post
    Ok, so i'm making a program where I have to update the user constantly, but I don't want the output to go over the console buffer space, so I thought if I printed something,
    Code:
    printf("%s", result);
    I need to erase it, and print the next thing.
    I tried
    Code:
    printf("%s", result);
    for (i = 0; i == strlen(result); i++)
    {
        printf("\b");
    }
    But that didn't work properly.
    There were still characters there when I printed the next bit of information.
    Any idea's?
    Thanks.
    How many times do you want your for loop to run? How many times is it actually going to run?

  4. #4
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    That varies.
    But the loop DOES run alot.
    Thousands of times.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bradszy View Post
    That varies.
    But the loop DOES run alot.
    Thousands of times.
    You would like to think so, I'd bet. However, I can promise you this: the loop never runs at all, at least if it's as you've written it here. Maybe if you had done <= strlen instead of ==, then it might run.

  6. #6
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Oh I thought you meant how many times does the loop it's IN run.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One other thing to keep in mind -- \b can only go backwards, it cannot go up. So it can't erase an \n, for example.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, \b is not an erase as such - it JUST means "move back one character". If you want to actually "blank" the space you went back to, you need to print "\b \b" - so you have x, then back over the x, print a space, then move back again.

    Or, you could use \r, which goes back to the start of the line, then print a number of spaces, and a \r again - that's a bit shorter string to manage.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Also, \b is not an erase as such - it JUST means "move back one character". If you want to actually "blank" the space you went back to, you need to print "\b \b" - so you have x, then back over the x, print a space, then move back again.

    Or, you could use \r, which goes back to the start of the line, then print a number of spaces, and a \r again - that's a bit shorter string to manage.

    --
    Mats
    I did something like that once in a test harness I wrote which kept updating the % completed. It worked great -- well until I started piping the output the a file at least.

  10. #10
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Thanks everyone, I got it done.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  2. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  3. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM
  4. How to Input without Breaking line in a console?
    By Aidman in forum C++ Programming
    Replies: 11
    Last Post: 03-12-2003, 11:55 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM