简体   繁体   English

clrscr(); Code::Blocks 中的等价物

[英]clrscr(); equivalent in Code::Blocks

how to clear the output console in code blocks??如何清除代码块中的输出控制台? why doesn't clrscr();为什么没有 clrscr(); work in Code::Blocks but works in Borland??在 Code::Blocks 中工作但在 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:最简单最直接的方法是通过system函数调用来完成:

#include <stdlib.h>

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

If you want to do it programmatically MSDN shows how here .如果您想以编程方式执行此操作, MSDN 在此处显示了方法

Note that there is no standard function provided by C++ for clearing the console.请注意,C++ 没有提供用于清除控制台的标准函数。 Some compilers, like borland, provides it as a non-standard function for convenience but it's not portable between different compilers.一些编译器,如 borland,为了方便起见,将其作为非标准函数提供,但它不能在不同编译器之间移植。

This is actually a quite simple problem.这实际上是一个非常简单的问题。 All you have to do is use printf.您所要做的就是使用 printf。 You don't even need printf or any headers, for that matter.就此而言,您甚至不需要 printf 或任何标题。

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

The \\e[1;1H sets the screen to the 1st row and 1st column. \\e[1;1H 将屏幕设置为第一行和第一列。 the 2J overwrites all characters currently on the screen. 2J 覆盖当前屏幕上的所有字符。

You can also use this:你也可以使用这个:

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

Then you don't need stdio.h.那你就不需要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.在包含 clrscr() 的头文件中创建自己的函数并使用它。 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)在“include”文件夹的路径中将其另存为“ClearScreen.h”(例如 - 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()现在,您所要做的就是调用clear_screen()

EDIT:编辑:

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

conio.h for linux用于 Linux 的 conio.h

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

copy to /usr/include复制到 /usr/include

sample:样品:

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

:) :)

tested by code::blocks in ubuntu 14.10在 ubuntu 14.10 中通过 code::blocks 测试

#include <stdlib.h>

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

or you can just add system("cls");或者你可以添加 system("cls"); in your main function.在您的主要功能中。 Note: it requires stdlib.h header file , so don't forget to include it.注意:它需要 stdlib.h 头文件,所以不要忘记包含它。

open conio.h in "MinGW\\include" folder just add these lines at very bottom and save conio.h在“MinGW\\include”文件夹中打开 conio.h 只需在最底部添加这些行并保存 conio.h

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

thats all now your clrscr();这就是你的 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来源: 实施了 clrscr 功能的程序

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

}

//try this and let me know if this works //试试这个,让我知道这是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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