有名管道读取写入

读取:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char const *argv[])
{
    int fd = open("/home/gec/pipe",O_RDWR);
    if(fd < 0)
    {
        perror("管道打开失败\n");
        return -1;
    }else
    {
        printf("管道打开成功\n");
    }

    while (1)
    {
        char buf[1024] = {0};
        read(fd,buf,1024);
        if(buf != "")
        {
            printf("%s\n",buf);
        }

    }
    





    return 0;
}

写入:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int fd = open("/home/gec/pipe",O_RDWR);
    if(fd < 0)
    {
        perror("管道打开失败\n");
        return -1;
    }else
    {
        printf("管道打开成功\n");
    }

    while (1)
    {
        char buf[1024] = {0};
        scanf("%s",buf);
        write(fd,buf,strlen(buf));
    }
    
    return 0;
}

效果:

总结:

 


版权声明:本文为aaa2540567665原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。