开发者

system() output to a char*

开发者 https://www.devze.com 2023-02-20 16:18 出处:网络
int system(const char *) How can I send output of this command (lets say the command is开发者_JS百科 \"pwd\") to a char*? Its returning an int but I want the results of the command to be sent to a c
int system(const char *)

How can I send output of this command (lets say the command is开发者_JS百科 "pwd") to a char*? Its returning an int but I want the results of the command to be sent to a char*.


You can pipe the output of the command directly to a file by using "pwd > tempfile" as command.
Another way is to use popen

FILE *output = popen("pwd", "r");

That will give you a file pointer where you can read the output from.


system("pwd > file");

system() uses "/bin/sh -c" under Linux so it can do anything a shell command line can do.

0

精彩评论

暂无评论...
验证码 换一张
取 消