在消息末尾获取不需要的字符
Getting unwanted characters at the end of a message
以下c 程序用于通过管道从父进程向子进程(使用fork() 创建)发送消息,并且运行 在linux 终端上!
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc,char *arg[]){
pid_t child;
int pipefd[2];
int ret;
char message[20];
ret =pipe(pipefd);
if((child=fork())==0){
printf("The child process \n");
close(pipefd[0]);
write(pipefd[1],"Hello from parent",17);
}
else{
close(pipefd[1]);
read(pipefd[0],message,17);
printf("Message from parent %s\n",message);
}
return 0;
}
上面的代码打印了消息 "Hello from parent" 但在父部分的末尾打印了一个 @ 符号!是什么原因,我该如何纠正?
同时发送字符串末尾的空字符。读书也一样。
write(pipefd[1],"Hello from parent",18);
以下c 程序用于通过管道从父进程向子进程(使用fork() 创建)发送消息,并且运行 在linux 终端上!
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc,char *arg[]){
pid_t child;
int pipefd[2];
int ret;
char message[20];
ret =pipe(pipefd);
if((child=fork())==0){
printf("The child process \n");
close(pipefd[0]);
write(pipefd[1],"Hello from parent",17);
}
else{
close(pipefd[1]);
read(pipefd[0],message,17);
printf("Message from parent %s\n",message);
}
return 0;
}
上面的代码打印了消息 "Hello from parent" 但在父部分的末尾打印了一个 @ 符号!是什么原因,我该如何纠正?
同时发送字符串末尾的空字符。读书也一样。
write(pipefd[1],"Hello from parent",18);