是 clrscr(); C++ 中的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/930138/
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
Is clrscr(); a function in C++?
提问by H4cKL0rD
I've looked everywhere for this function and cannot find the header files to make this work. It says clrscr() undeclared which brings me to the question. Is clrscr(); a function in C++?
我到处寻找这个函数,但找不到头文件来完成这项工作。它说 clrscr() 未声明,这让我想到了这个问题。是 clrscr(); C++ 中的函数?
回答by Pablo Santa Cruz
It used to be a function in <conio.h>, in old Borland C compilers.
它曾经是 <conio.h> 中的一个函数,在旧的 Borland C 编译器中。
It's not a C++ standard function.
它不是 C++ 标准函数。
回答by H4cKL0rD
And before someone posts the usual "please email me the conio.h file" request, can I point out that this ancient Borland header file only contained the declaration of the function. You would also need the supporting Borland library, which will not be compatible with any modern C++ compilation system.
在有人发布通常的“请给我发送 conio.h 文件”请求之前,我能否指出这个古老的 Borland 头文件只包含函数的声明。您还需要支持 Borland 库,它与任何现代 C++ 编译系统都不兼容。
回答by Ape-inago
As mentioned before, clrscr() is from turbo c++, inside conio.h
如前所述,clrscr() 来自 turbo c++,在 conio.h 中
For all intents and purposes, conio.h is "non standard", and as such should be probably avoided.
出于所有意图和目的,conio.h 是“非标准的”,因此可能应该避免。
I tend to use the precompiler to choose what to use for a simple clear screen, and just call the operating system's clear program.... it's smart enough to know how "tall" the screen is.
我倾向于使用预编译器来选择用于简单清晰屏幕的内容,然后调用操作系统的清晰程序......它足够聪明,知道屏幕有多“高”。
// somewhere in the program
#define WINDOWS 1
void console_clear_screen() {
#ifdef WINDOWS
system("cls");
#endif
#ifdef LINUX
system("clear");
#endif
}
In windows, you may want to look at the windows.h, You can interact with the windows console directly using a "handle", often noted in code as an hWin.
在 windows 中,您可能需要查看 windows.h,您可以使用“句柄”直接与 windows 控制台交互,通常在代码中标记为 hWin。
In linux, i've had good luck with curses/ncurses, although it is a little confusing at first.
在 linux 中,我在使用 curses/ncurses 时运气不错,尽管一开始有点令人困惑。
updateCalling system programs (clear.exe?)is a potential security risk - if someone is able to hiHyman the system call somehow thru an alternate avenue, they can force your program to do strange things. My recommendation is to dig into your platform's console api to get these things done.
更新调用系统程序(clear.exe?)是一个潜在的安全风险 - 如果有人能够通过替代途径以某种方式劫持系统调用,他们可以强制您的程序做奇怪的事情。我的建议是深入研究平台的控制台 api 来完成这些事情。
回答by Muhammad Akhtar
you have to include this header file for this function
你必须为这个函数包含这个头文件
#include <conio.h>
回答by Bruce
A web search says the header file you want is 'conio.h' - I haven't tried it out, so no guarantees. Its existence may also depend on what platform you are compiling against.
网络搜索说你想要的头文件是“conio.h”——我没有试过,所以不能保证。它的存在还可能取决于您正在编译的平台。
回答by r_goyal
you can use the system cls command to clear the output screen..
您可以使用 system cls 命令清除输出屏幕..
clrscr() is from turbo c++, inside conio.h and conio.h is "non standard", and as such should be probably avoided. example
clrscr() 来自 turbo c++,在 conio.h 中,conio.h 是“非标准的”,因此应该避免使用。例子
#include<windows.h>
main()
{
some code....;
system("cls");
some more code;
}
its tested and works.. i use dev c++ with mingw compiler.. :)
它经过测试并且有效..我将 dev c++ 与 mingw 编译器一起使用.. :)
回答by Kacper Pluta
On Linux I always use:
在 Linux 上我总是使用:
void clrscr(void)
{
fprintf(stdout, "3[2J"); // clean screen
fprintf(stdout, "3[1;1H"); // move cursor to the first line
}
回答by Eclipse
The easiest way to clear the screen in real C++ is to just send out a bunch of blank lines. Of course this is assuming that stdout is directed at the screen and not a file:
在真正的 C++ 中清除屏幕的最简单方法是发出一堆空行。当然,这是假设 stdout 是针对屏幕而不是文件:
for (int i = 0; i < 80; ++i)
cout << "\n";
cout << endl;
回答by EvilTeach
回答by Janus Troelsen
On Unix-like systems you can use VT100 escape codes.
在类 Unix 系统上,您可以使用 VT100 转义码。
std::cout << "3[2J" << std::flush;