首页 / 操作系统 / Linux / Linux C执行命令并保存到string中
- #include <stdio.h>
- #include <string.h>
-
- #include "cmd_exec.h"
-
- #include "comm.h"
-
- string ExecCmd(const string & cmd)
- {
- char store[kPipeMax], command[kCmdMax];
- string result;
- StrToCharStar(cmd, command);
- FILE *pfp = popen(command, "r");
- if (NULL == pfp)
- return result;
- memset(store, 0, kPipeMax);
- while (kPipeMax-1 == fread(store, sizeof(char), kPipeMax-1, pfp)
- && !feof(pfp)) {
- result += store;
- memset(store, 0, kPipeMax);
- }
- result += store;
-
- return result;
- }