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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17793059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:26:47  来源:igfitidea点击:

clrscr(); equivalent in Code::Blocks

c++codeblocks

提问by gandalf

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

如何清除代码块中的输出控制台?为什么没有 clrscr(); 在 Code::Blocks 中工作但在 Borland 中工作??

I have already tried:

我已经尝试过:

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

回答by greatwolf

The easiest most straightforward way is to just do it through systemfunction 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. Some compilers, like borland, provides it as a non-standard function for convenience but it's not portable between different compilers.

请注意,C++ 没有提供用于清除控制台的标准函数。一些编译器,如 borland,为了方便起见,将其作为非标准函数提供,但它不能在不同编译器之间移植。

回答by 112

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。就此而言,您甚至不需要 printf 或任何标题。

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.

\e[1;1H 将屏幕设置为第一行和第一列。2J 覆盖当前屏幕上的所有字符。

You can also use this:

你也可以使用这个:

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

Then you don't need stdio.h.

那你就不需要stdio.h了。

回答by Sidharth J

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

}

回答by Allen Linatoc

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 callclear_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

回答by Dalwadi Dilip

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

在包含 clrscr() 的头文件中创建自己的函数并使用它。例如 :

#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....

现在编译并运行它。我希望它有效....

回答by Shakiba Moshiri

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 | grep conio

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

:)

:)

tested by code::blocks in ubuntu 14.10

在 ubuntu 14.10 中通过 code::blocks 测试

回答by abe312

#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.

或者你可以添加 system("cls"); 在您的主要功能中。注意:它需要 stdlib.h 头文件,所以不要忘记包含它。

回答by Seyon Seyon

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(); will work

这就是你的 clrscr(); 将工作

回答by Cristian

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

int main()
{
 MyClearScreen();
}

回答by Alejandro Caro

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 功能的程序