C++ system() function — How to collect the output of the issued command?

前端 未结 8 1677
暖寄归人
暖寄归人 2020-12-03 16:14

I\'m running some commands with the C++ system() function:

int system ( const char * command );

How can I collect the standard

8条回答
  •  旧巷少年郎
    2020-12-03 17:02

    There are typically two ways for a system program to "return" a value: by writing to stdout, and by returning a status integer at the end of the program. (there are often more ways to return results, eg. by writing to a file or into a database, but I assume those are out of scope here).

    For receiving the status code, just check the return value of the system function.

    For receiving the output, either redirect it into a file, and read the file afterwards, or use popen.

提交回复
热议问题