콘솔을 클리어하려면
제목처럼.C++의 콘솔을 클리어하려면 어떻게 해야 하나요?
순수 C++의 경우
그럴수는 없어요.C++는 콘솔이라는 개념도 가지고 있지 않습니다.
프린터에 인쇄하거나, 파일에 직접 출력하거나, 다른 프로그램의 입력으로 리다이렉트 하거나 할 수 있습니다.C++에서 콘솔을 클리어해도 이러한 케이스는 상당히 복잡해집니다.
comp.lang.c++ FAQ에서 다음 항목을 참조하십시오.
OS 고유의
그래도 프로그램에서 콘솔을 클리어하는 것이 타당하고 운영체제 고유의 솔루션에 관심이 있는 경우 이러한 솔루션이 존재합니다.
(태그와 같이) Windows 의 경우는, 다음의 링크를 확인해 주세요.
에서 말한 입니다: 이 답변은 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 기왕이면 이렇게 됩니다.system("cls");
마이크로소프트가 그렇게 하라고 했기 때문입니다.그러나, 코멘트에서, 이것은 안전하지 않은 일이라고 지적되고 있다.이 문제로 인해 Microsoft 기사 링크를 삭제했습니다.
라이브러리(일부 휴대용)
ncurses는 콘솔 조작을 지원하는 라이브러리입니다.
- http://www.gnu.org/software/ncurses/ (Posix 시스템에 탑재)
- http://gnuwin32.sourceforge.net/packages/ncurses.htm(구 Windows 포트 추가)
Windows의 경우 콘솔 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);
}
가능한 오류는 모두 무시하지만 콘솔 클리어입니다.★★★★★★★★★★와 같지 않다.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";
}
「」를 사용합니다.system
이건 그냥 못생겼기 때문이야.
수레바퀴를 다시 만들지 않고도 내게 가장 쉬운 방법이야.
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
}
- Windows 에서는, 「conio」를 사용할 수 있습니다.h" header 및 call crscr 함수를 사용하여 시스템 기능을 사용하지 않도록 합니다.
#include <conio.h>
clrscr();
- Linux 에서는 ANSI 이스케이프 시퀀스를 사용하여 시스템 기능을 사용하지 않도록 할 수 있습니다.이 참조 ANSI 이스케이프 시퀀스를 확인합니다.
std::cout<< u8"\033[2J\033[1;1H";
- MacOS 탐색 중...
Linux/Unix 및 기타 일부의 경우 10TH2 이전 Windows의 경우:
printf("\033c");
는 단말기를 리셋합니다.
창 콘솔에 여러 줄을 출력해도 소용 없습니다.빈 행만 추가합니다. 안타깝게도 way는 윈도 고유의 방법으로 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 );
}
POSIX 시스템의 경우 ncurs 또는 터미널 기능을 사용할 수 있습니다.
#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" ) );
}
// #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;
}
화면을 지우려면 먼저 다음 헤더를 포함해야 합니다.
#include <stdlib.h>
그러면 Windows 명령어가 Import 됩니다.그런 다음 '시스템' 기능을 사용하여 배치 명령(콘솔 편집)을 실행할 수 있습니다.C++ 의 Windows 에서는, 화면을 클리어 하는 커맨드는 다음과 같습니다.
system("CLS");
그러면 콘솔이 비게 됩니다.전체 코드는 다음과 같습니다.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
system("CLS");
}
그리고 그게 네가 필요한 전부야!Good Luck :)
Windows 의 경우:
#include <cstdlib>
int main() {
std::system("cls");
return 0;
}
Linux/Unix의 경우:
#include <cstdlib>
int main() {
std::system("clear");
return 0;
}
이것은, 화면을 클리어 할 수 있는 Windows 기능에 액세스 할 수 없기 때문에, MAC 에서는 실시하기 어렵습니다.가장 좋은 방법은 단말기가 클리어될 때까지 루프를 하고 행을 추가한 후 프로그램을 실행하는 것입니다.그러나, 이것을 주로 자주 사용하는 경우는, 효율도 메모리 친화적이지 않습니다.
void clearScreen(){
int clear = 5;
do {
cout << endl;
clear -= 1;
} while (clear !=0);
}
사용하다system("cls")
화면을 지우려면:
#include <stdlib.h>
int main(void)
{
system("cls");
return 0;
}
Windows 에서는, 다음의 복수의 옵션이 있습니다.
clrscr() (헤더 파일: conio).h)
system("cls") (헤더 파일: stdlib).h)
Linux 에서는 system("clear") (header File : stdlib)를 사용합니다.h)
Windows 를 사용하고 있는 경우:
HANDLE h;
CHAR_INFO v3;
COORD v4;
SMALL_RECT v5;
CONSOLE_SCREEN_BUFFER_INFO v6;
if ((h = (HANDLE)GetStdHandle(0xFFFFFFF5), (unsigned int)GetConsoleScreenBufferInfo(h, &v6)))
{
v5.Right = v6.dwSize.X;
v5.Bottom = v6.dwSize.Y;
v3.Char.UnicodeChar = 32;
v4.Y = -v6.dwSize.Y;
v3.Attributes = v6.wAttributes;
v4.X = 0;
*(DWORD *)&v5.Left = 0;
ScrollConsoleScreenBufferW(h, &v5, 0, v4, &v3);
v6.dwCursorPosition = { 0 };
HANDLE v1 = GetStdHandle(0xFFFFFFF5);
SetConsoleCursorPosition(v1, v6.dwCursorPosition);
}
이는 시스템("cls")이 프로세스를 생성하지 않고도 수행할 수 있는 기능입니다.
뛰어난 동작:
#include <windows.h>
void clearscreen()
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
간단한 방법은 다음과 같습니다.
#include <iostream>
using namespace std;
int main()
{
cout.flush(); // Flush the output stream
system("clear"); // Clear the console with the "system" function
}
윈도우 10 터미널을 사용하고 있습니다.
std::system("cls"); // cls or clear
시스템 사용:: 콘솔:: Clear();
이렇게 하면 버퍼가 지워집니다(비웁니다).
#include <cstdlib>
void cls(){
#if defined(_WIN32) //if windows
system("cls");
#else
system("clear"); //if other
#endif //finish
}
임의의 장소에서 cls()를 호출한다.
용도: clrscr();
#include <iostream>
using namespace std;
int main()
{
clrscr();
cout << "Hello World!" << endl;
return 0;
}
가장 쉬운 방법은 스트림을 여러 번 플러시하는 것입니다(가능한 콘솔보다 큰 것이 이상적입니다). 1024*1024는 콘솔창이 없는 크기일 수 있습니다.
int main(int argc, char *argv)
{
for(int i = 0; i <1024*1024; i++)
std::cout << ' ' << std::endl;
return 0;
}
이 경우 유일한 문제는 소프트웨어 커서입니다.플랫폼/콘솔에 따라서는 콘솔의 맨 위가 아닌 맨 끝에 깜박이는 것(또는 깜박이지 않는 것)이 있습니다.그러나 이것이 결코 문제를 일으키지 않기를 바란다.
언급URL : https://stackoverflow.com/questions/6486289/how-can-i-clear-console
'programing' 카테고리의 다른 글
시스템 변환 방법창문들.'시스템'에 Input.Key를 입력합니다.창문들.양식. 열쇠? (0) | 2023.04.16 |
---|---|
Swift의 Int 선행 0 (0) | 2023.04.16 |
셸 스크립트의 행 끝에 세미콜론이 필요합니까? (0) | 2023.04.11 |
XlsxWriter로 쓴 후 셀에 형식 적용 (0) | 2023.04.11 |
x64 사용자 지정 클래스의 각 열거에 대한 버그 (0) | 2023.04.11 |