简体   繁体   English

如何在c文件中使用长命令运行系统函数?

[英]How to run system function with long command in c file?

I'm not able to get the output of command in text file when running this executable of '.c' file:运行“.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.

We can use system function to run the command as i mentioned in my question above.我们可以使用系统函数来运行我在上面的问题中提到的命令。

The only problem was that, the board on which i was running this application code required sudo permission for creating that file.唯一的问题是,我运行此应用程序代码的主板需要 sudo 权限才能创建该文件。

Just add 'sudo' permission as below : sprintf(image_upload_cmd, "sudo mcumgr conn show > /home/sample/statfile.txt");只需添加“sudo”权限如下: sprintf(image_upload_cmd, "sudo mcumgr conn show > /home/sample/statfile.txt");

This will redirect the output to the statfile.txt.这会将输出重定向到 statfile.txt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM