Thread: Erasing console

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Erasing console

    Is there a function that erases all data/text in a console window?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does shutdown() count?
    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.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a function that erases all data/text in a console window?
    I can think of two cases where that would make sense:

    1) You're looking at naughty text and your boss or parental unit is headed your way.
    2) You want to write annoyware that clears the output of previous programs.

    Either way, you're doing something wrong. Find another solution.
    My best code is written with the delete key.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    3) You want to display stats or some such in a window and want a clean window for it.

    If you want to do fancy console stuff, look into either a platform specific function/API, or for a more portable solution, PDCurses.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Ehhh... I wasn't sure how to word it and I knew it wouldn't come out so good, but back when I first ever took c++ in high school I remember we would always do programs that outputted in just a simple console window. I am practicing that again after such a long time and when the program gets so long it is annoying to me how it just keeps scrolling downward with text all over. Anyway, I could have sworn there was a function of some sort that erased or cleared the console window and then the rest of the program would then output and continue on, etc. If there isn't please correct me, but if there is... is there a function for C that does the same thing?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you're not going to get an answer until you say which OS / Compiler you're using.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is there a function for C that does the same thing?
    What compiler and OS are you using?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Sorry, OS is windows XP and compiler is visual studio.net 2003

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well how about that, there's a FAQ already.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Just scroll down to the one titled
    "WINDOWS CONSOLE OPTION: (Credit: Sunlight)"

    Feel free to point at every single function listed, then press F1 to read the fantastic manual on that function. Pretty soon, you'll get the idea about the whole of the win32 console API.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a function taken from the FAQ which will clear the screen. Just call it any time you want to clear the screen.
    Code:
    #include <windows.h> 
    
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    It's the last option under this FAQ entry: How do I... (Level 1) > Clear the screen?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Thanks all!

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >3) You want to display stats or some such in a window and want a clean window for it.
    There's a reason that shells support clearing the console directly. Be nice to your users and let them decide. If I want to clear the screen then I won't think twice if your program does it for me. But if I don't want to clear the screen, I'm going to be shopping for a new program shortly after you do it. Why? Mostly because you'll have annoyed me, but partially because a design like that is notoriously inflexible.
    My best code is written with the delete key.

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Options?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You wouldn't believe how truly annoying some programs which inappropriately clear the screen can be.

    Especially when you do things like redirection and pipes.
    annoyingprog > results.txt
    annoyingprog | filter > results.txt

    All manner of wierdness which needs to be filtered out - ugh!
    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.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Options?
    Don't clear the screen and keep the stats minimal. If you write to stdout without assuming that it's a console, you can use the program as a filter. Then I could search and sort your stats, I could save them to a file, send them to another program or across a network, I could clear the screen myself and send them to a formatter (or write my own to specialize the presentation!)... The options are almost overwhelming, all with a simple change, and it all falls out of removing a feature. See my signature and be enlightened.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM