Erasing console

This is a discussion on Erasing console within the C Programming forums, part of the General Programming Boards category; Is there a function that erases all data/text in a console window?...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,796
    >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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

Page 1 of 2 12 LastLast
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, 08: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, 01:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 02:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21