管道的进程间通信解决办法

管道的进程间通信
小白,求指教!
通过管道将一个文件中的内容读到另一个文件中!
读完之后 多出了 很多乱码!求前辈解答!代码如下:
/*将一个文件中的内容读到管道中*/
#include "comm.h"
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int fifo_fd=-1,read_fd=-1,fifoopen_fd=-1;
char buf[PIPE_BUF+1];
int res=0;


if((fifo_fd=mkfifo("/tmp/myfifo",0777))<0)
{
printf("Mkfifo failed!\n");
exit(1);
}


if((read_fd=open("Data.txt",O_RDONLY))<0)
{
printf("Open data.txt failed!\n");
exit(1);
}


if((fifoopen_fd=open("/tmp/myfifo",O_WRONLY))!=-1)
{
res=read(read_fd,buf,PIPE_BUF);
buf[PIPE_BUF]='\0';
while(res>0)
{
write(fifoopen_fd,buf,PIPE_BUF);
res=read(read_fd,buf,PIPE_BUF);
buf[PIPE_BUF]='\0';
}

}
close(read_fd);
close(fifoopen_fd);
return 0;
}


/*管道中的内容读到DataInfo.txt中*/
#include "comm.h"
int main()
{
int fifoopen_fd=-1;
int write_fd=-1;
int res=-1;
char buf[PIPE_BUF+1];


fifoopen_fd=open("/tmp/myfifo",O_RDONLY);
write_fd=open("DataInfo.txt",O_WRONLY|O_CREAT);

memset(buf,'\0',sizeof(buf));


if(fifoopen_fd!=-1)
{
do
{
res=read(fifoopen_fd,buf,PIPE_BUF);
write(write_fd,buf,res);
}while(res>0);
}else
{
exit(EXIT_FAILURE);
}
close(fifoopen_fd);
close(write_fd);
exit(EXIT_SUCCESS);
}





就是读完之后  源文件  和  目标文件大小不一样,而且查看目标文件,在读完正确内容后多了很多乱码!
------解决思路----------------------
你调试程序,看看究竟写了什么到文件中了
------解决思路----------------------
看它的返回值,是否出错,还有就是写了的内容是否在文件中。或许是因为缓冲中的数据被写进去了。

------解决思路----------------------
这句错了:write(fifoopen_fd,buf,PIPE_BUF);
应该是:write(fifoopen_fd,buf,res);