system() output to a char*

2019-07-11 21:36发布

int system(const char *)

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

标签: c file system
2条回答
We Are One
2楼-- · 2019-07-11 21:41

system("pwd > file");

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

查看更多
太酷不给撩
3楼-- · 2019-07-11 21:51

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.

查看更多
登录 后发表回答