#include
#include
#include
#include
#define MAXLINE 80
int main(void)
{
int n;
int fd[2];
pid_t pid;
char line[MAXLINE];
if(pipe(fd)<0)
{
perror("pipe");
exit(1);
}
pid=fork();
if(pid<0)
{
perror("can not open the pipe.");
exit(1);
}
else if(pid>0)
{
close(fd[0]);
write(fd[1],"hello! I and you\n",20);
close(fd[1]);
wait(NULL);
}
else
{
char t[15];
close(fd[1]);
n=read(fd[0],line,MAXLINE);
write(STDOUT_FILENO,line,n);
n=read(fd[0],NULL,MAXLINE);
sprintf(t,"It is %d \n",15);
printf("%s\n",t);
}
}
请问为什么使用printf或者write在终端显示时字符串的首字符总会多打一次,!谢谢!
图片说明