管道的简单应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwww_bw/article/details/53591301
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define fifo  "home/wenjian/FIFO"
int main()
{
    unlink("fifo");
   
    FILE *fp;
    int i;
    //char *str = "hello world";
    char str[100] ={0};
    i = mkfifo("fifo",O_CREAT|O_EXCL);
   
    if(i != 0)
    {
        printf("mkfifo error\n");
    }
    fp = fopen("fifo","w");
    if(fp == NULL)
    {
        printf("open error\n");
    }
   printf("please input str:\n");
    scanf("%s",str);
    fwrite(str,sizeof(str),1,fp);

    fclose(fp);
}


在一个终端中实现写入,另一个中实现读取、



#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define fifo  "home/wenjian/FIFO"
int main()
{
    FILE *tp;
    int src[100];
    int j;
//    unlink("fifo");
    j = mkfifo("fifo",O_CREAT);
    tp = fopen("fifo","w+");
   
    if(tp == NULL)
    {
        printf("open error\n");
    }
    fread(src,9,1,tp);
    printf("src = %s\n",src);
    fclose(tp);
}







猜你喜欢

转载自blog.csdn.net/wwwww_bw/article/details/53591301