Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to do pipes in C

Status
Not open for further replies.
Feb 17, 2003
3
0
0
GB
does anyone know where i can find some example codes of how to do something like the following in C :

input | process | output

i need to be able to supply the input to the process and read the output it gives onto a variable.

 
There is no special tricky things to do this.

On the most system, the pipe ( | ) or redirection ( < ) is the same than the standard IO of your C program.

you just have to remember than the IO is a text line. if you make a program (Ony) witch return the letter &quot;T&quot; or what ever converted to a char by the system ...
and you make a prog (Towy) which take a char and make an operation on it and return a double.

You can type Ony | Towy and it will work.

Ony will write to the standard output &quot;T&quot;, this come from the line return &quot;T&quot; or printf(&quot;T&quot;); in your ony.c program.

It will try to write in on the screen but the system catch it with the pipe and send it in Towy

If in Towy you have a sort of getch, witch is suppose to read from the standard input (keyboard), it will receve the &quot;T&quot; from Ony by the pipe. and it will run.

I hope you anderstand me, my english might be not so good.

I hope I answers the question, so I don't think you will find some exemples because programs like ony.c and towy.c won't have some particular things.
It works with the standard IO.

If you want more help, you can be more precise by writing a small exemple.

hope it helps and I didn't miss your question ...

lcout
 
What platform?
For *nix try man popen and man fifo.
 
Oh ...

Ok ... anyway I am sick today ... it is my excuse ... I should not answers ... marsd is right ... I absolutly didn't get the question in fact ...

here a small example with my apologize : about popen

#include <stdio.h>

int main (int argc, char **argv)
{
FILE* flux;
char tmp[256];

flux = popen(&quot;ls -a&quot;, &quot;r&quot;);
while(fgets(tmp, 256, flux))
printf(&quot;>> %s&quot;, tmp);

pclose(flux);
return 0;
}

it should put a >> before every line ls -a give

lcout
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top