Mer*_*ham 62
对于纯C++
你不能.C++甚至没有控制台的概念.
该程序可以打印到打印机,直接输出到文件,或重定向到另一个程序的输入,以满足它的所有需要.即使您可以在C++中清除控制台,也会使这些情况变得更加混乱.
请参阅comp.lang.c ++ FAQ中的此条目:
操作系统相关
如果清除程序中的控制台仍然有意义,并且您对特定于操作系统的解决方案感兴趣,那么这些解决方案确实存在.
对于Windows(如标记中所示),请查看此链接:
编辑:此答案之前提到使用system("cls");,因为微软说这样做.但是在评论中已经指出这不是一件安全的事情.由于此问题,我已删除了Microsoft文章的链接.
图书馆(有点便携)
ncurses是一个支持控制台操作的库:
Cat*_*lus 47
对于Windows,通过Console API:
void clear() {
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
SetConsoleCursorPosition(console, topLeft);
}
Run Code Online (Sandbox Code Playgroud)
它很高兴忽略了所有可能的错误,但是嘿,这是控制台清理.不喜欢system("cls")处理错误更好.
对于*nixes,您通常可以使用ANSI转义码,因此它是:
void clear() {
// CSI[2J clears screen, CSI[H moves the cursor to top-left corner
std::cout << "\x1B[2J\x1B[H";
}
Run Code Online (Sandbox Code Playgroud)
使用system它只是丑陋.
Jom*_*oma 25
对我来说最简单的方法而不必重新发明轮子。
void Clear()
{
#if defined _WIN32
system("cls");
//clrscr(); // including header file : conio.h
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
system("clear");
//std::cout<< u8"\033[2J\033[1;1H"; //Using ANSI Escape Sequences
#elif defined (__APPLE__)
system("clear");
#endif
}
Run Code Online (Sandbox Code Playgroud)
#include <conio.h>
clrscr();
Run Code Online (Sandbox Code Playgroud)
std::cout<< u8"\033[2J\033[1;1H";
Run Code Online (Sandbox Code Playgroud)
NoA*_*gel 16
对于Linux/Unix以及其他一些但在10 TH2之前不适用于Windows:
printf("\033c");
Run Code Online (Sandbox Code Playgroud)
将重置终端.
将多行输出到窗口控制台是没有用的..它只是向其中添加空行。可悲的是,方法是特定于Windows的,并且涉及conio.h(和clrscr()可能不存在,也不是标准标头)或Win API方法
#include <windows.h>
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
Run Code Online (Sandbox Code Playgroud)
对于POSIX系统,它更简单,您可以使用ncurses或终端函数
#include <unistd.h>
#include <term.h>
void ClearScreen()
{
if (!cur_term)
{
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result <= 0) return;
}
putp( tigetstr( "clear" ) );
}
Run Code Online (Sandbox Code Playgroud)
小智 5
要清除屏幕,您首先需要包含一个模块:
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)
这将导入 Windows 命令。然后您可以使用“系统”功能运行批处理命令(编辑控制台)。在 C++ 的 Windows 上,清除屏幕的命令是:
system("CLS");
Run Code Online (Sandbox Code Playgroud)
这将清除控制台。整个代码如下所示:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
system("CLS");
}
Run Code Online (Sandbox Code Playgroud)
这就是你所需要的!祝你好运 :)
小智 5
// #define _WIN32_WINNT 0x0500 // windows >= 2000
#include <windows.h>
#include <iostream>
using namespace std;
void pos(short C, short R)
{
COORD xy ;
xy.X = C ;
xy.Y = R ;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE), xy);
}
void cls( )
{
pos(0,0);
for(int j=0;j<100;j++)
cout << string(100, ' ');
pos(0,0);
}
int main( void )
{
// write somthing and wait
for(int j=0;j<100;j++)
cout << string(10, 'a');
cout << "\n\npress any key to cls... ";
cin.get();
// clean the screen
cls();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
234923 次 |
| 最近记录: |