int system(const char *)
Run Code Online (Sandbox Code Playgroud)
如何将此命令的输出(假设命令为"pwd")发送到char*?它返回一个int,但我希望将命令的结果发送到char*.
您可以使用"pwd> tempfile"作为命令将命令的输出直接传递给文件.
另一种方法是使用popen
FILE *output = popen("pwd", "r");
这将为您提供一个文件指针,您可以从中读取输出.
system("pwd> file");
system()在Linux下使用"/ bin/sh -c",因此它可以执行shell命令行可以执行的任何操作.