#include <stdio.h>
#include <stdlib.h>
int main() {
char command[1024]; // 存放要运行的shell命令
FILE* pipe; // 管道文件指针
sprintf(command, "pwd"); // 设置要运行的shell命令为"pwd"(列出当前路径)
if ((pipe = popen(command, "r")) == NULL) {
printf("无法打开管道\n");
return 1;
}
char buffer[1024]; // 读取输出内容的缓冲区
while (fgets(buffer, sizeof(buffer), pipe)) {
printf("%s", buffer); // 将每一行输出到控制台上
}
pclose(pipe); // 关闭管道
return 0;
}
linux C语言 调用命令行 管道文件指针
最新推荐文章于 2024-07-12 11:34:40 发布