1. 读取外部程序的输出
我们在程序中用popen访问uname命令给出的信息。命令uname -a的作用是打印系统信息,包括计算机型号、操作系统名称、版本和发行号、以及计算机网络名。
完成程序的初始化工作后,打开一个连接到uname命令的管道,把管道设置为可读方式并让read_fp指向该命令的输出。最后,关闭read_fp指向的管道。
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE *read_fp;
char buf[1024];
int chars_read;
memset(buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
read_fp = popen( "uname -a", "r" ); //将“uname -a”命令的输出通过管道读取(“r”参数)到FILE\* stream
if(read_fp != NULL)
{
chars_read = fread( buf, sizeof(char), sizeof(buf), read_fp);//将数据流读取到buf中
if(chars_read >0)
printf("my output:\n%s\n",buf);
pclose( read_fp );
}
return 0;
}
2. 将输出送往外部程序
这里将数据写入管道,使用的额是od(八进制输出)的命令。
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE *write_fp;
char buf[1024];
int chars_write;
memset(buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
sprintf(buf,"hello world...\n");
write_fp = popen( "od -c", "w" ); //通过管道(“w”参数)写入到FILE\* stream
if(write_fp != NULL)
{
fwrite( buf, sizeof(char), sizeof(buf), write_fp); //将FILE\* write\_fp的数据流写入到buf中
pclose( write_fp );
}
return 0;
}
程序使用带有参数“w”的popen启动od -c命令,这样就可以向该命令发送数据了。然后它给od -c命令发送一个字符串,该命令接收并处理它,最后把处理结果打印到自己的标准输出上。
在命令行上,我们可以使用下面命令得到同样的输出结果:
echo “hello world…\n” | od -c
三、返回值分析
和system调用类似,也需要考虑调用返回值。popen执行一个 shell 以运行命令来开启一个进程。pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态。如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。
这里重点参考一位博主的文章,首先要明确几点:
- pclose 失败返回 -1, 成功则返回 exit status, 同 system 类似,需要用 WIFEXITED, WEXITSTATUS 等获取命令返回值。
- 和 system 一样,SIGCHLD 依然会影响 popen,如果设置了SIGCHLD 则获取不到子进程的状态。
- 管道只能处理标准输出,不能处理标准错误输出。
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
int main(int argc, char* argv[])
{
char cmd[1024];
char line[1024];
FILE* pipe;
int rv;
if (argc != 2)
{
printf("Usage: %s <path>\n", argv[0]);
return -1;
}
// pclose fail: No child processes
//signal(SIGCHLD, SIG\_IGN);
snprintf(cmd, sizeof(cmd), "ls -l %s 2>/dev/null", argv[1]);
pipe = popen(cmd, "r");
if(NULL == pipe)
{
printf("popen() failed: %s\n", cmd);
return -1;
}
while(fgets(line, sizeof(line),pipe) != NULL)
{
printf("%s", line);
}
rv = pclose(pipe);
if (-1 == rv)
{
printf("pclose() failed: %s\n", strerror(errno));
return -1;
}
if (WIFEXITED(rv))
{
printf("subprocess exited, exit code: %d\n", WEXITSTATUS(rv));
if (0 == WEXITSTATUS(rv))
{
// if command returning 0 means succeed
printf("command succeed\n");
}
else
{
if(127 == WEXITSTATUS(rv))
{
printf("command not found\n");
return WEXITSTATUS(rv);
}
else
### 最后的话
最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!
### 资料预览
给大家整理的视频资料:
![](https://img-blog.csdnimg.cn/img_convert/433fbed363c5c2d3a33eac159b4c99f7.png)
给大家整理的电子书资料:
![](https://img-blog.csdnimg.cn/img_convert/566a0a4a03cf3395c585d3995850fb98.png)
**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**
# 资料预览
给大家整理的视频资料:
[外链图片转存中...(img-zdZ8rshe-1718778212703)]
给大家整理的电子书资料:
[外链图片转存中...(img-HkS0rknr-1718778212703)]
**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**