functioncommandoutput-redirectc-libraries

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:

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.

Solution

  • 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.

    Just add 'sudo' permission as below : sprintf(image_upload_cmd, "sudo mcumgr conn show > /home/sample/statfile.txt");

    This will redirect the output to the statfile.txt.