Code Sample |
#include<stdio.h> #include<string.h> int main(void){ int childpid,fd[2],nb,i,j; char line[BUFSIZ]="I want to print this line twice"; char word[BUFSIZ]; 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]); for( i=0;i<7;i++){ nb=read(fd[0],word,sizeof(word)); printf("%s\n",word); } } return 0; } |
Quote (dtf @ April 27 2006,23:54) |
If you don't mind me speculating a little. |
Quote |
I believe the problem is in the nb=read(fd[0],word,sizeof(word)); You are thinking that you are reading one line incrementing the file pointer then reading a second etc. But I think you are possibly reading in the entire message and what is in the word buffer is the entire phrase I<null>want<null>to<null>print<null>this<null>line<null&a mp;g t;twice<null> and when you do the printf you alway print from the first word "I". |
Quote |
Try dumping the contents of the word buffer character by character to see what is in there after the first read |
Code Sample |
for (k=0;k<strlen(word);k++) printf("%c",word[k]); |