-----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int main(void)
{
FILE *fp;
//命令执行的结果放置在fp指向的结构体缓冲中
fp = popen("cat /etc/passwd","r");
char buf[512];
memset(buff,0,sizeof(buf));
while(fgets(buf,sizeof(buf),fp) != NULL)
{
printf("%s",buf);
}
pclose(fp);
printf("------------------------------\n");
//为wc命令提供统计的数据
fp = popen("wc -l","w");
//向fp指向的结构体缓存中写入数据
fprintf(fp,"1\n2\n3\n");
pclose(fp);
exit(0);
}
时间: 2024-10-18 13:14:23