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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to pipe in both direction

Status
Not open for further replies.
Feb 17, 2003
3
0
0
GB
using the following program i am trying to feed a certain external process with its input and take its output and store the whole output onto a variable. i am from a perl background, usually in perl we can use |s easily with filehandles. can anyone point out what i did wrong below?


// script to test piping IOs
#include <fstream>

using std::cout;
using std::cin;
using std::ios;
using std::fstream;

int main() {
char buffer[256];
fstream PipeProcess(&quot;| /script_that_takes_input_then_gives_output |&quot;, ios::in | ios::eek:ut);

if (!PipeProcess) {
cerr << &quot;file could not be opened.&quot; << endl;
exit(1);
}

cout << &quot;my input to this script&quot;;

// read the output from the script
while (!PipeProcess.eof())
{
PipeProcess.getline(buffer,256);
cin << buffer;
}
return 0;
}
~
~
 
Pipe always only works in one direction.... Sounds like your trying to take your input feed it to another program and read it back... What you'll want to do is:


Code:
system(&quot;/*aplication*/ > temp.txt&quot;);
fstream fin;
fin.open(&quot;temp.txt&quot;);
//do stuff
fin.close();
sytem(rm temp.txt&quot;);
 
You could use a FIFO to read the data back, this is some psuedo code:
[tt]mkfifo(&quot;fifo&quot;);
program = popen(&quot;command > fifo&quot;, &quot;w&quot;);
output = open(&quot;fifo&quot;, &quot;r&quot;);
write(program, &quot;things&quot;);
read(output, buffer);
pclose(program);
close(output);[/tt] //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top