当前位置: 首页 > news >正文

做网站的收益象山专业网站建设

做网站的收益,象山专业网站建设,网站建设与维护报告总结,小米商城网页设计论文1. 关键词2. sysutil.h3. sysutil.cpp 3.1. system_util_unix.cpp3.2. system_util_win.cpp 4. 测试代码5. 运行结果6. 源码地址 1. 关键词 关键词#xff1a; C 系统调用 system popen 跨平台 应用场景#xff1a; 希望直接调用操作系统的某些命令#xff0c;并获取命…1. 关键词2. sysutil.h3. sysutil.cpp 3.1. system_util_unix.cpp3.2. system_util_win.cpp 4. 测试代码5. 运行结果6. 源码地址 1. 关键词 关键词 C 系统调用 system popen 跨平台 应用场景 希望直接调用操作系统的某些命令并获取命令的返回值。 2. sysutil.h #pragma once#include cstdint #include stringnamespace cutl {/*** brief Execute a system command.** param cmd the command to be executed.* return true if the command is executed successfully, false otherwise.*/bool system(const std::string cmd);/*** brief Execute a system command and get the output.** param cmd the command to be executed.* param result the output of the command.* return true if the command is executed successfully, false otherwise.*/bool callcmd(const std::string cmd, std::string result); } // namespace cutl 不需要返回值时可以直接使用system 需要获取返回值时可以调用callcmd。 3. sysutil.cpp #include map #include iostream #include strutil.h #include cstdlib #include sysutil.h #include inner/logger.h #include inner/system_util.h #include inner/filesystem.hnamespace cutl {bool system(const std::string cmd){return call_system(cmd);}bool callcmd(const std::string cmd, std::string result){// 读取命令执行结果的最大Buffer长度constexpr int MAX_CMD_BUF_LEN 1024;FILE *fp pipline_open(cmd);if (fp NULL){CUTL_ERROR(pipline_open error for cmd: cmd);return false;}//  读取命令执行结果char buffer[MAX_CMD_BUF_LEN] {0};char *res fgets(buffer, sizeof(buffer), fp);if (res NULL){CUTL_ERROR(read result error for cmd: cmd);if (pipline_close(fp) ! 0){CUTL_ERROR(pipline_close error for cmd: cmd);}return false;}if (pipline_close(fp) ! 0){CUTL_ERROR(pipline_close error for cmd: cmd);}result strip(std::string(buffer));return true;} } // namespace cutl 3.1. system_util_unix.cpp #if defined(_WIN32) || defined(__WIN32__) // do nothing #else#include system_util.h #include inner/logger.hnamespace cutl {bool call_system(const std::string cmd){if (cmd.empty()){CUTL_ERROR(cmd is empty!);return false;}pid_t status;status std::system(cmd.c_str());if (-1 status){CUTL_ERROR(system error!);return false;}if (!WIFEXITED(status)){CUTL_ERROR(exit status: std::to_string(WEXITSTATUS(status)));return false;}if (0 ! WEXITSTATUS(status)){CUTL_ERROR(run shell script fail, script exit code: std::to_string(WEXITSTATUS(status)));return false;}return true;}FILE *pipline_open(const std::string cmd){return popen(cmd.c_str(), r);}int pipline_close(FILE *stream){return pclose(stream);}} // namespace cutl#endif // defined(_WIN32) || defined(__WIN32__) 3.2. system_util_win.cpp #if defined(_WIN32) || defined(__WIN32__)#include cstdlib #include system_util.h #include inner/logger.hnamespace cutl {// https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/reference/system-wsystem?viewmsvc-170bool call_system(const std::string cmd){if (cmd.empty()){CUTL_ERROR(cmd is empty!);return false;}int ret system(cmd.c_str());if (ret ! 0){CUTL_ERROR(std::string(system failure, error) strerror(errno));return false;}return true;}// https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/reference/popen-wpopen?viewmsvc-170FILE *pipline_open(const std::string cmd){return _popen(cmd.c_str(), r);}// https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/reference/pclose?viewmsvc-170int pipline_close(FILE *stream){return _pclose(stream);}} // namespace cutl#endif // defined(_WIN32) || defined(__WIN32__) 4. 测试代码 #include common.hpp #include sysutil.hvoid TestSystemCall() {PrintSubTitle(TestSystemCall);bool ret cutl::system(echo hello);std::cout system call echo hello, return: ret std::endl;auto cmd cmake --version;std::string result_text;ret cutl::callcmd(cmd, result_text);std::cout callcmd cmd , return: ret std::endl;std::cout callcmd cmd , result text: result_text std::endl; } 5. 运行结果 -------------------------------------------TestSystemCall------------------------------------------- hello system call echo hello, return: 1 callcmd cmake --version, return: 1 callcmd cmake --version, result text: cmake version 3.28.3 6. 源码地址 更多详细代码请查看本人写的C 通用工具库: common_util, 本项目已开源代码简洁且有详细的文档和Demo。 本文由博客一文多发平台 OpenWrite 发布
http://www.pierceye.com/news/766574/

相关文章:

  • 企业网站手机版商城网站有什么好处
  • 推荐一本学做网站的书温州阀门网站建设
  • 用户要承担暖气费的税吗太原优化排名推广
  • Wordpress外贸网站搭建公司宿迁建设网站
  • 学校网站建设的意义和应用山东淄博网站建设
  • 莱芜网站建设价格低网站vps
  • 长治做网站哪家好赣州注册公司
  • 网站开发从入门到精通做h5的网站哪个好
  • 免费公司网站如何建立设计个人网站好备案吗
  • 建网站和做微信哪个好在线识别图片百度识图
  • php网站开发如何实现删除功能大连大连建设工程信息网站
  • 表格模板免费下载网站wordpress 插件位置
  • wordpress小白能学会吗汕头做网站优化公司
  • 军队营房基础建设网站重庆做网站个人
  • 网站建设怎样中英文网站备案是空间备案还是域名备案
  • 陕西网站制作人力资源服务外包
  • 成都网站建设哪家售后好网站建设费可以计业务费吗
  • 做服装到哪个网站拿货品质好自己制作的网页别人如何访问
  • 榆林哪里做网站网页游戏网站那个好
  • 泰安口碑好的企业建站公司wordpress验证码无效
  • 圣矢网络重庆网站建设优化推广公司好听好记的网站域名
  • 如何做旅游小视频网站比较好的外贸公司
  • 图书馆建设投稿网站使用 ahrefs 进行 seo 分析
  • 校园网站建设 德育免费换ip软件
  • 排行网站模板凡科代理千万不要做
  • 贵州省冶金建设有限公司网站网站好玩新功能
  • 怎么让客户做网站惠州关键词排名提升
  • 创建公司网站需要什么国外的智慧城市建设网站
  • 阿里云服务器做网站django高清无版权网站
  • 网页制作与网站制作wordpress二次元风格