我提供的代码如下,自己补充main函数哈,希望能够帮到你:)
//相关头文件:
#include
#include
#include
#include
#include
#include
#include
发送方send:
void fifo_pro()
{
char s[128];
int fd;
FILE *fp;
fp = fopen("./a.txt", "r");
mkfifo("/tmp/fifo.tst", 0644);
fd = open("/tmp/fifo.tst", O_WRONLY);
while(fgets(s, 127, fp) != NULL) {
write(fd, s, strlen(s));
//printf("%s",s);
}
close(fd);
fclose(fp);
unlink("/tmp/fifo.tst");
}
接收方get:
char s[128];
int fd = open("/tmp/fifo.tst", O_RDONLY);
int fd2 = open("./b.txt", O_WRONLY)
memset(s, 0, 128);
while(read(fd, s, 128) > 0) {
printf("%s", s);
write(fd2, s, 128);
}
close(fd2);
close(fd);
get端程序
--------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
char s[128];
int fd,fd2;
int len;
fd = open("/tmp/fifo.tst", O_RDONLY,0);
fd2 = open("./b.txt", O_WRONLY|O_CREAT,777);
if(fd<0||fd2<0)
{
printf("errer%d\t%d\n",fd,fd2);
exit(0);
}
memset(s, 0, 128);
do{
len=read(fd, s, 128);
write(fd2, s,len);
}while(len>0);
close(fd2);
close(fd);
}
------------------------------------
send端程序
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
char s[128];
int fd,fp;
int len;
fp = open("./a.txt", O_RDONLY,0);
unlink("/tmp/fifo.tst");
mkfifo("/tmp/fifo.tst",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
fd = open("/tmp/fifo.tst", O_WRONLY,0);
if(fd<0||fp<0)
{
printf("errer%d\t%d\n",fd,fp);
return 0;
}
do{
len=read(fp,s, 127) ;
write(fd, s,len);
printf("%s",s);
}while(len>0);
close(fd);
close(fp);
return 0;
}
--------------------------------------------