zoukankan      html  css  js  c++  java
  • c++程序判断系统是Linux还是Windows

      用C++来实现,本来想了很多,后来越写越烂,而且结果总是不尽人意,干脆这样子好了:

    1 int main() {
    2     int judge = system("cls");
    3     if(judge == 0)     cout << "Windows!" << endl;
    4     else    cout << "Linux!" << endl;
    5     return 0;
    6 }

      只是 linux 下会多了一行东西,如果运行时在背后加个 2>> error 也是可以的,不过好像用户体验还是不好,为了去掉这个,我又想了如何囧办法:

    (感觉好像有点小题大做了,不过确实实现了功能,就不管了,或许以后能用上)

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 const char *password = "123";
     5 
     6 int main(int argc, char *argv[]) {
     7 
     8     if(argc != 2 || strcmp(argv[1], password) != 0) {
     9         puts("Permission denied!");
    10         return 2;
    11     }
    12     freopen("err", "w", stderr);
    13     FILE *fp = fopen("out", "w");
    14     int judge = system("cls");            // 0 为 Windows,>0 为 Linux
    15     fprintf(fp, "%d
    ", judge == 0 ? 0 : 1);
    16     fclose(fp);
    17     return 0; 
    18 }
    create.cpp
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 const char *password = "123";
     5 
     6 int main(int argc, char *argv[]) {
     7 
     8     if(argc != 2 || strcmp(argv[1], password) != 0) {
     9         puts("Permission denied!");
    10         return -1;
    11     }
    12     
    13     char cmd[20] = "create ";
    14     system(strcat(cmd, password));
    15     FILE *fp = fopen("out", "r");
    16     int judge;
    17     fscanf(fp, "%d", &judge);
    18     fclose(fp);
    19 
    20     if(judge == 0) {
    21         system("del out");
    22         system("del err");
    23         return 0;
    24     }
    25     else if(judge > 0) {
    26         system("rm -f out");
    27         system("rm -f err");
    28         return 1;
    29     }
    30     else    return -1;
    31 }
    getSystem.cpp
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 
     5 inline int getSystem() {
     6     return system("getSystem 123");
     7 }
     8 
     9 int main() {
    10     
    11     int res = getSystem();
    12     
    13     if(res == 0)    puts("Windows");
    14     else    puts("Linux");
    15     
    16     return 0;
    17 }
    main.cpp

      都编译好后运行 main.exe 或者 main.out 就行,另两个.exe 已加密(密码可以自行更改),这样子直接就可以避免直接点击运行他们。

  • 相关阅读:
    python利用ffmpeg工具将视频帧推流至rtsp
    高斯曲线拟合
    Fast角点检测
    Harris角点检测和ShiTomasi角点检测
    python赋值,浅拷贝,深拷贝
    自适应阈值二值化
    Queue模块
    threading模块
    pycharm2020.3.3安装使用教程(2021.5.2更新)
    值得推荐的C/C++框架和库
  • 原文地址:https://www.cnblogs.com/Newdawn/p/5054409.html
Copyright © 2011-2022 走看看