linux 下mkfifo ,读管道,所得字符串总是乱码
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#define FIFO "/mnt/hgfs/ShareFiles/lab3/fifo"
int main()
{
char *buf;
int fd;
unlink( FIFO );
mkfifo( FIFO, 0777 );
if ( fork() > 0 )
{
char *s = "HelloWorld";
fd = open( FIFO, O_WRONLY );
write( fd, s, strlen(s)+1 );
close( fd );
}
else
{
fd = open( FIFO, O_RDONLY );
read( fd, buf, sizeof(buf) );
printf(" %s\n", buf );
close( fd );
}
return 0;
}
这是个在简单不过的代码 ,可是编译过后,运行就是乱码,这个怎么解决呢,望DX指教