• Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by rip-off
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.

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 ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement