fifo

来源:互联网 发布:蒙古 欧洲 知乎 编辑:程序博客网 时间:2024/04/27 14:21

read data:


/*
 * mypipe.c
 *
 *  Created on: 2014-7-30
 *      Author: root
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include<fcntl.h>

int main()
{
    int len = 0;

    char buf[100];
    memset(buf,0,sizeof(buf));
    int fd = open("fifo1",O_RDONLY);
    while((len = read(fd,buf,sizeof(buf)))>0)
    {
        printf("%s",buf);//错误点在此行,如果%s后不加\n,则导致读取数据窗口没有变化,只有等写窗口关闭,读窗口才全部显示出来
    }
    close(fd);
    return 0;



}



writedata:


/*
 * writepipe.c
 *
 *  Created on: 2014-7-30
 *      Author: root
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include<fcntl.h>

int main(int arg,char *args[])
{


    int len=0;
    char buf[100];
    memset(buf,0,sizeof(buf));
    int fd = open("fifo1",O_WRONLY);
    while(1)
    {

        scanf("%s",buf);

        if(buf[0] == '0')
            break;

        write(fd,buf,sizeof(buf));
    }
    close(fd);
    return 0;

}



0 0
原创粉丝点击