📄 popen1.c
字号:
/************
// name : popen1.c
// author : pyy
// date : 2007-11-23
**************/
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char buf[501];
int ch;
memset(buf,'\0',sizeof(buf));
printf("read from pipe ...\n");
/* building pipe*/
fp = popen("ls -l","r");
if (fp!=NULL)
{
/* 将ls执行的结果存入buf变量中*/
ch = fread(buf,sizeof(char),500,fp);
if (ch>0)
{printf("\n 文件列表如下:\n"); printf("%s\n",buf);}
pclose(fp);
}
else
{
printf("can't open");
return 1;
}
printf("\n 写入 二进制文件.....\n");
/* test.bin*/
fp=fopen("test.bin","wb");
if(fp == NULL)
{
printf("can't find the file.\n");
return 1;
}
/* buf -> test.bin*/
fwrite(buf,sizeof(char),500,fp);
fclose(fp);
printf("\n读取二进制文件.\n");
/* 清除 buf 变量内容 */
memset(buf,'\0',sizeof(buf));
/* open test.bin */
fp = fopen("test.bin","rb");
if (fp == NULL)
{ printf(" cant open file.\n"); return 1;}
/** 将test.bin 内容读入,存入buf 变量中**/
fread(buf,sizeof(char),500,fp);
fclose(fp);
printf("\n write to pipe ...\n\n");
/** 建立写入管道 **/
fp = popen("grep fifo","w");
/** 将buf变量内容写入管道,运行grep 命令**/
fwrite(buf,sizeof(char),500,fp);
pclose(fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -