dtf
Group: Members
Posts: 196
Joined: Nov. 2005 |
|
Posted: April 28 2006,13:10 |
|
This will work.
Code Sample | dtf@eliza:~/myprograms/test1/src> cat pipes.c #include<stdio.h> #include<string.h>
int main(void){ int childpid,fd[2],nb,i,j; char line[128]="I want to print this line twice"; char word[128]; char *bufptr;
pipe(fd);
childpid=fork();
if(childpid==0) { printf("from child:\n");
close(fd[0]);
char *token=strtok(line," ");
while(token!=NULL) { printf(" %s\n",token);
write(fd[1],token,(strlen(token)+1));
token=strtok(NULL," ");
}
}
else {
wait(NULL); printf("from parent:\n");
close(fd[1]);
nb=read(fd[0],word,sizeof(word));
bufptr = word;
for( i=0;i<7;i++){
printf("%s\n",bufptr);
bufptr = bufptr + strlen(bufptr); bufptr++;
}
}
return 0;
}
|
Does this make sense?
Below is the complied output
dtf@eliza:~/myprograms/test1/src> gcc -o pipes pipes.c dtf@eliza:~/myprograms/test1/src> ./pipes from child: I want to print this line twice from parent: I want to print this line twice dtf@eliza:~/myprograms/test1/src>
The only reason I changed BUFSIZ to 128 was so I could see what I was working with. You can leave your code as is.
|