Jak wyczyścić ekran konsoli w C?
Czy istnieje "właściwy" sposób na wyczyszczenie okna konsoli w C, poza użyciem system("cls")
?
12 answers
Cóż, C nie rozumie pojęcia ekranu. Więc każdy kod nie byłby przenośny. Może Zerknij na conio.h lub przekleństwa , zgodnie z Twoimi potrzebami?
Przenośność jest problemem, bez względu na to, jaka Biblioteka jest używana.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-28 06:18:22
printf("\e[1;1H\e[2J");
Ta funkcja będzie działać na terminalach ANSI, wymaga POSIX. Zakładam, że istnieje wersja, która może również działać na konsoli window, ponieważ obsługuje również sekwencje escape ANSI.
#include <unistd.h>
void clearScreen()
{
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12);
}
Są inne alternatywy, z których niektóre nie przesuwają kursora do {1,1}.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-01 05:52:25
Skoro wspominasz cls
, brzmi jakbyś odnosił się do systemu windows. Jeśli tak, to Ten KB element ma kod, który to zrobi. Po prostu próbowałem i zadziałało, gdy zadzwoniłem do niego z następującym kodem:
cls( GetStdHandle( STD_OUTPUT_HANDLE ));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-27 16:01:28
#include <conio.h>
I używać
clrscr()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-18 06:53:57
Obejście Przetestowane w systemie Windows (cmd.exe), Linux (Bash i zsh) oraz OS X (zsh):
#include <stdlib.h>
void clrscr()
{
system("@cls||clear");
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-31 10:48:35
Nie ma na to sposobu. Chociaż różne biblioteki manipulacji kursorami, takie jak klątwy, są stosunkowo przenośne. Conio.h jest przenośny pomiędzy OS / 2 DOS i Windows, ale nie do * Nix.
Całe pojęcie "konsoli" jest pojęciem wykraczającym poza zakres standardu C.
Jeśli szukasz czystego rozwiązania API Win32, nie ma jednego wywołania w interfejsie API konsoli Windows, aby to zrobić. Jednym ze sposobów jest FillConsoleOutputCharacter z wystarczająco duża liczba znaków. Lub WriteConsoleOutput możesz użyć GetConsoleScreenBufferInfo aby dowiedzieć się, ile znaków wystarczy.
Możesz również utworzyć całkowicie nowy Bufor ekranu konsoli i utworzyć bieżący.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-28 00:41:47
Używając makr możesz sprawdzić, czy korzystasz z systemu Windows, Linux, Mac lub Unix i wywołać odpowiednią funkcję w zależności od bieżącej platformy. Coś w następujący sposób:
void clear(){
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif
#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-27 22:14:01
Dla przenośności, spróbuj tego:
#ifdef _WIN32
#include <conio.h>
#else
#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")
#endif
Następnie po prostu zadzwoń clrscr()
. W systemie Windows będzie używał conio.h
'S clrscr()
, a w Linuksie będzie używał kodów ucieczki ANSI.
Jeśli naprawdę chcesz zrobić to "prawidłowo", możesz wyeliminować pośredników (conio
, printf
, itd.) i zrobić to tylko za pomocą niskopoziomowych narzędzi systemowych (przygotuj się na ogromny zrzut kodu): {]}
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#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 );
}
#else // !_WIN32
#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" ) );
}
#endif
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-19 17:25:42
Windows:
system("cls");
Unix:
system("clear");
Możesz zamiast tego wstawiać znaki nowej linii, dopóki wszystko nie zostanie przewinięte, spójrz tutaj .
Dzięki temu można łatwo osiągnąć przenośność.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-27 18:13:58
Wystarczy wpisać clrscr (); function w void main ().
Jako przykład:
void main()
{
clrscr();
printf("Hello m fresher in programming c.");
getch();
}
clrscr();
Funkcja łatwa do wyczyszczenia ekranu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-18 11:00:11
Właściwym sposobem jest użycie funkcji tput
LUB terminfo
w celu uzyskania właściwości terminala, a następnie wstawiania nowych linii zgodnie z wymiarami..
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-27 15:22:55
To powinno zadziałać. Następnie po prostu wywołaj cls (); gdy chcesz wyczyścić ekran.
(stosując metodę zaproponowaną wcześniej.)
#include <stdio.h>
void cls()
{
int x;
for ( x = 0; x < 10; x++ )
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-01 22:37:29