# pipe(), popen(), FILE*, file descriptors and fstream - how to use?

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Goal: have my program run another program and be able to send data to its STDIN and read from its STDOUT. I would like to have control over the IO with a fstream opposed to a FILE* that popen() makes. I started with this:
int runApp (char * dynCmd) {
FILE * my_pipe;
my_pipe = popen( dynCmd, "r" );
if (my_pipe == NULL) return -1;
char buf[SIZE];
while ( fgets(buf, SIZE, my_pipe) != NULL )
cout << buf;
return 0;
}
1) popen() is unidirectional. I need biderectional 2) popen() gives a FILE* and not a fstream. How do I go about running an app such that I can get input and output with a fstream? (Or a ifstream + ofstream)

##### Share on other sites
I doubt there is any standard way to handle this. If you were more specific about the platforms you target maybe we could give you some advice.

For example on linux you would use the pipe() call, followed by fork()ing and close()ing the child processes stdin and stdout, then calling dup() on the pipe read and write descriptors, before exec*()ing the process. The parent can read and write to its pipe descriptors for the communication.

On windows there is probably a very similar process, create some kind of interprocess communication handle and passing its input and output ends via the "startup info" parameter...

There may be ways of getting a std::i/o stream from a file pointer (Google tells me that the gnu c++ library has at least an extension tp obtain a std::stream_buf from FILE *). I don't know of any standard way, that said I haven't delved too deeply into some of c++'s stream stuff, there could well be.

##### Share on other sites
rip-off is completely right; there is no standard way at all to do this. That having been said, the Boost library might be handy for the I/O part (see here).

##### Share on other sites
Quote:
 Original post by rip-offI doubt there is any standard way to handle this. If you were more specific about the platforms you target maybe we could give you some advice. For example on linux you would use the pipe() call, followed by fork()ing and close()ing the child processes stdin and stdout, then calling dup() on the pipe read and write descriptors, before exec*()ing the process. The parent can read and write to its pipe descriptors for the communication.On windows there is probably a very similar process, create some kind of interprocess communication handle and passing its input and output ends via the "startup info" parameter...There may be ways of getting a std::i/o stream from a file pointer (Google tells me that the gnu c++ library has at least an extension tp obtain a std::stream_buf from FILE *). I don't know of any standard way, that said I haven't delved too deeply into some of c++'s stream stuff, there could well be.

OS: Solaris (Unix-like)
Someone else also told me to set it up with pipe(), fork(), execl?() etc.
Getting a stream_buf from a FILE * is good. Converting stream_buf to a fstream is trivial. I'll Google. Thanks.

EDIT: I think I'll be reading stdio.h ;)

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 76
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009585
• ### Who's Online (See full list)

There are no registered users currently online

×