执行命令并获得输出:popen和pclose undefined
Execute a command and get output: popen and pclose undefined
正如在这个答案中解释的那样,我尝试在visual studio项目中使用c++执行windows命令并获得输出。
std::string executeCommand (const char* cmd) {
char buffer[128];
std::string result = "";
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
问题是,这不是编译。给出了以下错误:
Error: identifier "popen" is undefined.
Error: identifier "pclose" is undefined.
由于使用了Microsoft编译器,因此需要在popen
和pclose
的开头使用下划线。替换:
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
:
std::shared_ptr<FILE> pipe(_popen(cmd, "r"), _pclose);
相关文章:
- 自动"yes"到 Popen SSH 命令
- pclose() 不会给我进程退出代码
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 从popen()读取的输出是否在pclose()之前完成
- Popen使Qt调试器崩溃
- 为什么if(fork()==0){getpid()}和popen()进程返回相同的进程id
- popen() 如何工作以及如何在 Linux 上实现它到 C++ 代码中
- C++将 popen 错误输出读取为字符串
- 如何重定向 stderr 并使用 popen() 在后台运行
- popen() 将执行命令的输出写入 cout
- C++相当于从 popen 阅读的 fgets s
- 为什么返回的 FILE* 在 popen() 中的命令失败时保留数据
- Python 子进程.Popen 在 g++ 中编译,自动添加.cpp文件
- POPEN 可以找出是否安装了某些应用程序吗?
- 参数太多,无法在C++中'FILE* popen()'发挥作用
- 使用Popen函数或使用非编译库在C 中运行SSH
- 如何防止在C++中使用 popen() 打开的进程接收 SIGINT 信号?
- 非静态成员函数的使用无效(while loop/no class/wget popen)
- 做一个popen(),将文件*指针放入fstream中,pclose()呢?
- 执行命令并获得输出:popen和pclose undefined