【问题标题】:How to run system function with long command in c file?如何在 c 文件中使用长命令运行系统功能?
【发布时间】:2020-04-09 22:52:31
【问题描述】:

运行“.c”文件的可执行文件时,我无法在文本文件中获取命令的输出:

char image_upload_cmd[100] = {'\0'};

sprintf(image_upload_cmd, "mcumgr conn show > /home/sample/statfile.txt");
system(image_upload_cmd);

--> mcumgr command exists in '/usr/bin/'.
--> conn, show are options to that command.

【问题讨论】:

    标签: function command output-redirect c-libraries


    【解决方案1】:

    我们可以使用系统函数来运行我在上面的问题中提到的命令。

    唯一的问题是,我运行此应用程序代码的开发板需要 sudo 权限才能创建该文件。

    只需添加 'sudo' 权限,如下所示: sprintf(image_upload_cmd, "sudo mcumgr conn show > /home/sample/statfile.txt");

    这会将输出重定向到 statfile.txt。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2017-05-16
      • 2019-10-25
      相关资源
      最近更新 更多