因为有些控制字符会被错误处理。比如二进制文件中的0,在传输的时候会被当作字符串结束符,这样是无法传送的。所以传输二进制文件的时候状况与文本文件是不一样的。
我提供的代码如下,自己补充main函数哈,希望能够帮到你:)//相关头文件:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
发送方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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)