Sign in to follow this  

redirecting output to a file using STL

This topic is 4597 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

Ok, so here's what I'm trying to accomplish: Attach an std::ofstream created in my running app to the stdout of a process launched from my app. I've searched around google.com and have only found ways to do this via Win32 HANDLE's. As far as I know, STL fstreams don't support file descriptors (although I read that they used to or at least some implementations do/used to) so it looks like going that route is out of the question. Also, fstream's tie() method works only with ostreams which doesn't help me (or does it?). Has anybody ever done this or maybe has some useful input about my problem?

Share this post


Link to post
Share on other sites
It's not something that's covered in C++ since it is a very system-specific thing.

It looks like you're asking two questions, not one.

1) How do I send my output stream to another process,
2) How do I make that other process send the output to stdout.

For the first question, there are several ways to go about IPC (InterProcess Communication). Windows supports 9 different methods, and of those, I would suggest a pipe for what you describe. I'm not aware of any pipe wrapper class that acts as an ostream/istream pair for the two processes.

For part one, the running app should send the data through the pipe to the launched process.

For part two, the launched process should output the data by reading from the pipe and writing to stdout.

Sorry I can't offer a "Here's the function you didn't see."

frob

Share this post


Link to post
Share on other sites
If it was in the same process, you could use rdbuf and swap the target streambuf. You could still do it this way with a custom streambuf (or filebuf) that uses a socket to send the data to a remote streambuf/filebuf in the other process.

tie is for syncronization between, say, input and output streams using the same console (e.g. cin & cout). I think you would need this if you remoted both the input and output.

You know, C++ really could really use a 'socketbuf' that uses tcp. You might even be able to find one out there.

Share this post


Link to post
Share on other sites
@frob: Thanks for your suggestions. The use of pipes is how it is currently being done but I was hoping for a nicer looking solution. What I also wanted to achieve was to make it as platform independent as possible... oh well :)

@magmai: Reading through The Standard C++ Library by Nicolai Josuttis, he shows how you can use tie() to connect std::cout to an ofstream which seemed like what I wanted. Unfortunately, there seems to be no way for me to retrieve a different processes std::cout with which to tie() it to. Agreed, it would be nice to have a socketbuf to attach to an fstream. Might be a worthwhile project for myself to explorer tcp and how to define custom streambuf's.

What I finally ended up doing was taking advantage of the fact that I have cygwin installed and do something like: D:\myperl.pl 1>targetfile.bin 2>error.log.

Perhaps not the greatest solution but preferable (imo) to dealing with windows pipes.

Share this post


Link to post
Share on other sites

This topic is 4597 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this