C ++中的清除屏幕命令

时间:2012-03-07 08:11:19

标签: c++ windows-7 mingw codeblocks

我想在用户在C ++中输入一些数字后清除屏幕。我正在控制台应用程序模式下编程。

那怎么办呢?我的操作系统是win7,我的IDE是CodeBlocks,而编译器是MingW ......

7 个答案:

答案 0 :(得分:5)

这取决于你的操作系统, 如果你使用linux:

system("clear");

如果你使用windows:

system("cls"); 

但这会让您的应用程序变得便携,最好是

cout << string(50, '\n');

此行将打印行似乎终端已“清除”。

关于该问题的好文章: http://www.cplusplus.com/articles/4z18T05o/

答案 1 :(得分:3)

您可以使用clrscr()中定义的conio.h。但是为什么不问你之前谷歌?

ways to clear screen the out put screen

答案 2 :(得分:2)

你可以试试系统方法例如系统( “CLS”);

答案 3 :(得分:1)

在编译器中链接conio.h。我忘了怎么做。如果你将使用清晰的屏幕重复这个功能。

enter code here
void clrscr()
{
  system("cls");
}

答案 4 :(得分:1)

这就是微软有关清理控制台的说法:

#include <windows.h>

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   DWORD dwConSize;

   // Get the number of character cells in the current buffer.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write
                                    coordScreen,     // Coordinates of first cell
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute
                                    coordScreen,      // Coordinates of first cell
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}

int main()
{
    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);
    return 0;
}

答案 5 :(得分:1)

  

系统( “CLS”);

     
    

辉煌。那么如果我用我自己的恶意cls替换Windows cls会发生什么?你刚刚给了我控制权,谢谢!这就是所谓的后门,你用一种不安全的技术将它打开了。

  

来源:http://www.daniweb.com/software-development/cpp/threads/76934/how-do-i-clear-my-screen-in-c

答案 6 :(得分:0)

一种方法是输出'\ f'(对应于ASCII换页符,代码12,行打印机用来弹出页面,并被一些常见的终端和模拟器识别为清晰的屏幕)。 / p>

这不适用于Windows。

#ifdef _WIN32
/* windows hack */
#else
std::cout << '\f' std::flush;
#endif
相关问题