| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2103 人关注过本帖
标题:linux 中怎么清空fifo管道的数据?
只看楼主 加入收藏
fuxingC80
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-7-27
收藏
 问题点数:0 回复次数:0 
linux 中怎么清空fifo管道的数据?
想在两个程序间通信,但管道的数据会一直结尾追加,不能清空从头开始发数据,求大佬!

读端:
int main(){
    char *filename = "/home/hi/testprog/my_fifo";
    int fd;
    char buf_r[100];
    int nread;
    int i = 0;
    fd = open(filename,O_RDONLY);
    if(fd == -1){
        printf("open file fail!!\n");
        return 0;
    }
    printf("hello , start to test reading!!!\n");
    while(1)
    {
        memset(buf_r,0x00,sizeof(buf_r));
        if((nread = read(fd, buf_r, 100)) == -1)
        {

        printf("从FIFO读取的数据为:%s\n", buf_r);
        for(i;i<20;i++){
            printf("%d",buf_r[i]);
        }        
        i = 0;
        fflush(stdout);
        sleep(1);
    }   
    return 0;
}

写端:
int main(){
    char *c;
    char *str;
    str = malloc(100*sizeof(char*));
    c = malloc(100*sizeof(char*));
    char *filename = "/home/hi/testprog/my_fifo";
    int res = mkfifo(filename,0777);   
    int fd;
    if(res != 0){
        fprintf(stderr,"fifo creat file fail!!\n");
        exit(EXIT_FAILURE);
    }
    fd = open(filename,O_WRONLY);
    if(fd == -1){
        printf("open file fail!!\n");
        return 0;
    }
    printf("hello , start test to writing!!!\n");
    while(1)
    {   
        fflush(stdout);
        while((*c = getchar()) != '\n'){
            strcat(str,c);
        }
        write(fd,str,100);
        printf("end of write!\n");
        sleep(1);
    }
    return 0;
}
搜索更多相关主题的帖子: open char 数据 int printf 
2020-07-27 15:37
快速回复:linux 中怎么清空fifo管道的数据?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017093 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved