[]通过无名管道从父进程向子进程发消息乱码有关问题

[求救]通过无名管道从父进程向子进程发消息乱码问题
建立无名管道,父进程发送消息,子进程接收消息,子进程打印接收到的消息,显示乱码

不知道是什么原因,求救啊??


输出结果如下
[gpadmin@db2serv1 weiming]$ ./a.out 
[child]length=[5] buf=[HelloN=?燄縘

程序源码:
C/C++ code

#include<unistd.h>
#include<string>
#include<string.h>
#include<stdio.h>

int main()
{
    pid_t pid;
    int fildes[2];
    char buf[256];
    int i,j;
    
    if( pipe(fildes) < 0 )
    {
        printf("pipe error\n");
        return -1;
    }
    
    if ( (pid = fork()) < 0)
    {
        printf("fork error!\n");
        return -1;
    }
    
    if(0 == pid)
    {
        //子进程
        close(fildes[1]);
        j = read(fildes[0],buf,sizeof(buf));
        printf("[child]length=[%d] buf=[%s]\n",j,buf);
        return 0;
    }
    
    //父进程
    close(fildes[0]);
    write(fildes[1],"Hello",strlen("Hello"));
    
    return 0;
}




------解决方案--------------------
j = read(fildes[0],buf,sizeof(buf));
printf("[child]length=[%d] buf=[%s]\n",j,buf);

没有 \0 结束符
buf[j] = '\0';
加上这句