简体   繁体   中英

clrscr(); equivalent in Code::Blocks

how to clear the output console in code blocks?? why doesn't clrscr(); work in Code::Blocks but works in Borland??

I have already tried:

cout << "\x1b[2J\x1b[1;1H" << flush;
cls() ;

The easiest most straightforward way is to just do it through system function call:

#include <stdlib.h>

int main()
{
  system("cls");
}

If you want to do it programmatically MSDN shows how here .

Note that there is no standard function provided by C++ for clearing the console. Some compilers, like borland, provides it as a non-standard function for convenience but it's not portable between different compilers.

This is actually a quite simple problem. All you have to do is use printf. You don't even need printf or any headers, for that matter.

printf("\e[1;1H\e[2J");

The \\e[1;1H sets the screen to the 1st row and 1st column. the 2J overwrites all characters currently on the screen.

You can also use this:

write(0,"\e[1;1H\e[2J",12);

Then you don't need stdio.h.

You can use the OS commands to clear the contents of the console.

#include<stdlib.h>
int main(){

system("cls");   //For windows
system("clear"); //For Linux

}

Create own function in own header file which contain clrscr() and use it. for example :

#include <stdlib.h>
void clrscr()
{
   system("cls");
}

save it as "ClearScreen.h" at your path of "include" folder (for ex - C:\\Program Files (x86)\\CodeBlocks\\MinGW\\include)

compile it.

now use it in your programs like :

#include <stdio.h>
#include <conio.h>
#include <ClearScreen.h>

main()
{
   clrscr();
   printf("\nHi");
   getch();
   return 0;
}

now compile and run it. i hope it works....

I just searched on the internet.

Can't remember the source, but this should be the most accurate so far.

#include<windows.h>    

void clear_screen ()
{
    DWORD n;                         /* Number of characters written */
    DWORD size;                      /* number of visible characters */
    COORD coord = {0};               /* Top left screen position */
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    /* Get a handle to the console */
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

    GetConsoleScreenBufferInfo ( h, &csbi );

    /* Find the number of characters to overwrite */
    size = csbi.dwSize.X * csbi.dwSize.Y;

    /* Overwrite the screen buffer with whitespace */
    FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
    GetConsoleScreenBufferInfo ( h, &csbi );
    FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

    /* Reset the cursor to the top left position */
    SetConsoleCursorPosition ( h, coord );
}

Now, all you have to do is call clear_screen()

EDIT:

Just found the source: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385

conio.h for linux

download: http://sourceforge.net/projects/conio4linux/?source=typ_redirect

copy to /usr/include

sample:

root@shu-GA-VT890P:/usr/include# ls | grep conio

:)

tested by code::blocks in ubuntu 14.10

#include <stdlib.h>

int main()
{
  system("cls");
}

or you can just add system("cls"); in your main function. Note: it requires stdlib.h header file , so don't forget to include it.

open conio.h in "MinGW\\include" folder just add these lines at very bottom and save conio.h

#include <stdlib.h>
void clrscr()
{
   system("cls");
}

thats all now your clrscr(); will work

void function MyClearScreen()
{
 asm
    {
     mov ax,0600h;  
     mov bh,71h;    
     mov cx,0000h;  
     mov dx,184Fh;  
     int 10h;       
    };
};

int main()
{
 MyClearScreen();
}

Try this (It is shorter than the one above):

#include <windows.h>

void clrscr()/*Create funcion to clean screen.*/
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hStdOut, &csbi);
    FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
    SetConsoleCursorPosition(hStdOut, coord);
}

Source: Program with clrscr funtion implemented

#include<conio.h>
#include<iostream.h>
int main()
{
//using namespace std
 clrscr();

}

//try this and let me know if this works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM