Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: C problem

  1. #1
    New Coder
    Join Date
    Nov 2003
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C problem

    Hey, what is the function to clear the command window in C from everything thats been printed in it?

    Thanks for the help,
    Lippy

  • #2
    New Coder
    Join Date
    Nov 2004
    Location
    Netherlands
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use the non-ANSI standard function clrscr(), but it would be better to make one yourself. Or, if you'd rather, here's a function I modified from someone else to do the same. You'd pass it the handle to the console output (ie. HANDLE console_out_h = GetStdHandle(STD_OUTPUT_HANDLE))
    Code:
    #include <windows.h>
    
    int ClearScreen(HANDLE console_out)
    {
    	DWORD writ, len;
    	CONSOLE_SCREEN_BUFFER_INFO console_buffer_info;
    	COORD start = {0, 0};
    	
    	GetConsoleScreenBufferInfo(console_out, &console_buffer_info);
    	FillConsoleOutputCharacter(console_out, (TCHAR)32, 
    		(console_buffer_info.dwSize.X * console_buffer_info.dwSize.Y), start, 
    		&writ);
    	
    	GetConsoleScreenBufferInfo(console_out, &console_buffer_info);
    	FillConsoleOutputAttribute(console_out, console_buffer_info.wAttributes, 
    		(console_buffer_info.dwSize.X * console_buffer_info.dwSize.Y), start, 
    		&writ);
    	
    	SetConsoleCursorPosition(console_out, start);
    	
    	return 0;
    }

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Something like that is probably the best way *if* you're using windows.

    If you compiler has it..
    Code:
    #include <conio.h>
    
    int main()
    {
        clrscr();
    
        return 0;
    }

    A quick way I sometimes use is the system() command..
    Code:
    #include <stdlib.h>
    
    int main()
    {
        system("cls"); // windows
    
        system("clear"); // gnuc
    
        return 0;
    }

  • #4
    New Coder
    Join Date
    Nov 2003
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot for the help... Thats exactly what I needed

    Lippy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •