如此回答所解释的那样,我尝试在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.