开发者

how to check if the OS is Windows or Debian in c++?

开发者 https://www.devze.com 2022-12-25 13:31 出处:网络
I want to clear console screen every time the user make an input in C++. I\'m thinking of using system command. For Windows, it is \"cls\". For Linux, it is \"clear\". Is there a way check which syst

I want to clear console screen every time the user make an input in C++.

I'm thinking of using system command. For Windows, it is "cls". For Linux, it is "clear". Is there a way check which system to use the appropriate command in c++?

Than开发者_如何学Cks.


There is a trick solution:

if (system("clear"))
  system("cls");

For Unix it just working properly. For Windows, it will prompt an error like

'clear' is not recognized as an internal or external command, operable program or batch file.

While it will be cleared right now by "cls". So in the Command Prompt Window, it will not leave any track. :D


No, there isn't a C++ Standards way to do it.

You can instruct your windows compiler to define the WINDOWS macro and your linux compiler to define the LINUX macro though; some compilers do this by default (for example Visual Studio defines WIN32).


You could use C Preprocessor - Conditional Syntax in C++ too.

#ifdef linux
    //clear
#else
    //cls
#endif
0

精彩评论

暂无评论...
验证码 换一张
取 消