⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 popen1.c

📁 嵌入式Linux程序设计与应用案例 电子书源码 中国电力出版社
💻 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 + -