设计两个程序,要求用命名管道fifo,实现简单的文本文件或图片文件的传输功能 25
2个回答
展开全部
读取文件写入命名管道:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
int main(int argc,char *argv[]){
mkfifo("tp",0644);
int infd;
infd = open("test",O_RDONLY);
if(-1 == infd){
perror("open");
exit(1);
}
int outfd;
outfd = open("tp",O_WRONLY);
if(-1 == outfd){
perror("open");
exit(1);
}
char buf[1024];
int n;
while((n = read(infd,buf,1024)) > 0){
write(outfd,buf,n);
}
//关闭读端写端
close(infd);
close(outfd);
return 0;
}
读取管道写入目标文件:
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
int main(int argc,char *argv[]){
//写端
int outfd;
outfd = open("target.bak",O_WRONLY | O_CREAT | O_TRUNC,0644);
if(-1 == outfd){
perror("open");
exit(1);
}
int infd;
infd = open("tp",O_RDONLY);
if(-1 == infd){
perror("open");
exit(1);
}
char buf[1024];
int n;
while((n = read(infd,buf,1024))>0){
write(outfd,buf,n);
}
close(infd);
close(outfd);
unlink("tp");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询