如 this answer 中所述,我试图在 visual studio 项目中执行一个 windows 命令并在 C++ 中获取输出。
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.
最佳答案
由于使用微软编译器,popen
和pclose
开头需要下划线。替换:
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
与:
std::shared_ptr<FILE> pipe(_popen(cmd, "r"), _pclose);
关于c++ - 执行命令并获得输出 : popen and pclose undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876218/