Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (2) </ 1 [2] >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: pipe< Next Oldest | Next Newest >
dtf Offline





Group: Members
Posts: 196
Joined: Nov. 2005
Posted: April 28 2006,13:10 QUOTE

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.
Back to top
Profile PM 
smart girl Offline





Group: Members
Posts: 4
Joined: Mar. 2006
Posted: April 28 2006,13:31 QUOTE

yes as you said
when I do dummping I get the full line

thanx alot :)
Back to top
Profile PM 
6 replies since April 27 2006,23:32 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (2) </ 1 [2] >/
reply to topic new topic new poll
Quick Reply: pipe

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code