#include <iostream>
#include <cstdlib>
int main() {
// 打开命令管道
const char* command = "cmd.exe";
FILE* pipe = popen(command, "r");
if (pipe == NULL) {
std::cerr << "popen failed, error code: " << errno << std::endl;
return 1;
}
// 向命令提供输入
char input[] = "echo Hello, world!";
std::fwrite(input, sizeof(input), 1, stdin);
// 从命令读取输出
char buffer[128];
std::string result;
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
result += buffer;
}
// 关闭管道
pclose(pipe);
std::cout << result << std::endl;
return 0;
}
它会一直卡在while循环里面出不去,fgets的返回值一直不为null,不知道为什么