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

Started by
2 comments, last by yitzle 17 years ago
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)
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.
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).
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 ;)

This topic is closed to new replies.

Advertisement