1. #ifdef WIN32
  2. #include <Windows.h>
  3. #else
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #endif
  7. #include <assert.h>
  8. std::string getCurrentAppPath()
  9. {
  10. #ifdef WIN32
  11. char path[MAX_PATH + 1] = {0};
  12. if (GetModuleFileName(NULL, path, MAX_PATH) != 0)
  13. return std::string(path);
  14. #else
  15. char path[256] = {0};
  16. char filepath[256] = {0};
  17. char cmd[256] = {0};
  18. FILE* fp = NULL;
  19. // 设置进程所在proc路径
  20. sprintf(filepath, "/proc/%d", getpid());
  21. // 将当前路径设为进程路径
  22. if(chdir(filepath) != -1)
  23. {
  24. //指定待执行的shell 命令
  25. snprintf(cmd, 256, "ls -l | grep exe | awk '{print $10}'");
  26. if((fp = popen(cmd,"r")) == NULL)
  27. {
  28. return std::string();
  29. }
  30. //读取shell命令执行结果到字符串path中
  31. if (fgets(path, sizeof(path)/sizeof(path[0]), fp) == NULL)
  32. {
  33. pclose(fp);
  34. return std::string();
  35. }
  36. //popen开启的fd必须要pclose关闭
  37. pclose(fp);
  38. return std::string(path);
  39. }
  40. #endif
  41. return std::string();
  42. }
  43. std::size_t getCpuCount()
  44. {
  45. #ifdef WIN32
  46. SYSTEM_INFO sysInfo;
  47. GetSystemInfo(&sysInfo);
  48. return sysInfo.dwNumberOfProcessors;
  49. #else
  50. long cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
  51. if (cpu_num == -1)
  52. {
  53. assert(false);
  54. return 0;
  55. }
  56. // 看两者是否相等
  57. assert(cpu_num == sysconf(_SC_NPROCESSORS_CONF));
  58. return cpu_num;
  59. #endif
  60. }

windows和linux下获取当前程序路径以及cpu数的更多相关文章

  1. 在Windows及Linux下获取毫秒级运行时间的方法

    在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...

  2. Linux下获取当前程序的绝对路径

    在Linux开发应用时,我们常常需要在程序中获取当前程序绝对路径,我们可以通过readlink读取符号链接/proc/self/exe进行获取,这个符号链接代表当前程序,它的源路径就是当前程序的绝对路 ...

  3. 怎样在windows下和linux下获取文件(如exe文件)的具体信息和属性

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xmt1139057136/article/details/25620685 程序猿都非常懒.你懂的! ...

  4. windows和linux下查看java安装路径

    windows下查看版本:(默认安装路径安装就不需要去配环境变量了) java -version windows下查看安装路径: java -verbose Linux下安装版本查看方式和window ...

  5. 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别

    原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...

  6. php windows与linux下的路径区别

    php windows与linux下的路径区别windows用的是"\",linux用的是"/"这一点要特别清楚,, ps:在PHP windows也可以用/表 ...

  7. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  8. 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH

    谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH  PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...

  9. paip兼容windows与linux的java类根目录路径的方法

    paip兼容windows与linux的java类根目录路径的方法 1.只有 pathx.class.getResource("")或者pathx.class.getResourc ...

随机推荐

  1. externn "C"解析

    1.揭密extern "C" extern "C"包含双重含义,从字面上即可得到:首先,被它修饰的目标是 "extern”的:其次,被它修饰的目标是 ...

  2. [python] 如何用python操作Excel

    直接上代码: from openpyxl import Workbook from openpyxl.cell import get_column_letter wb = Workbook() des ...

  3. 实测Eclipse连接小米2S调试问题

    小米2S手机在Eclipse真机调试时,设备选择列表无法显示手机,DDMS也连接不上设备,解决步骤: 1.打开手机设置中开发者选项 - USB调试开启: 2.保证小米2S手机Windows下设备驱动已 ...

  4. android开发的问题集(二)

    (1)子线程对UI线程操作的简便方法 子线程方法用 Looper.prepare(); 结束时候用 Looper.loop();  

  5. 杭电oj 3079 Vowel Counting

    Tips:可以先将输入的字符串全部转化为小写字母,然后再将元音字母变为大写,时间复杂度O(n) #include<stdio.h> #include<string.h> #in ...

  6. SoftLAyer VPN

    1,安装softlayer-VPN(即跑VPN客户端)的机器与在SoftLAyer中的HardwareHostServers or VMIServer的privateIP互通

  7. 剑指offer57 删除链表中重复的结点

    class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(!pHead) return pHead; str ...

  8. IKAnalyzer使用停用词词典进行分词

    @Test // 測试分词的效果,以及停用词典是否起作用 public void test() throws IOException { String text = "老爹我们都爱您.&qu ...

  9. ios5和ios6横竖屏支持及ipad和iphone设备的判断

    ios5和ios6横竖屏支持及ipad和iphone设备的判断 判断是ipad还是iphone设备.此定义在PayViewControllerDemo-Prefix.pch 定义如下: #define ...

  10. 【深搜加剪枝】【HDU1455】【Sticks】

    题目大意:有一堆木棍 由几个相同长的木棍截出来的,求那几个相同长的木棍最短能有多短? 深搜+剪枝 具体看代码 #include <cstdio> #include <cstdlib& ...