Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2][Windows] Cannot redirect stdout to a file #412

Open
TonyMSantander opened this issue Oct 18, 2024 · 1 comment
Open

[v2][Windows] Cannot redirect stdout to a file #412

TonyMSantander opened this issue Oct 18, 2024 · 1 comment

Comments

@TonyMSantander
Copy link

TonyMSantander commented Oct 18, 2024

I am using boost process v2 from the 1.86 version of the boost library.

The following code is intended to redirect stdout to a file. It creates the file but the contents of stdout are not written to it; the file size is zero. When I redirect stdout to a pipe, the pipe contains the expected result.

I guessed at how to write the code to redirect stdout to a file, as I could not find any example in the documentation or online. So I have probably not done it right.

#include <boost/process/v2.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/readable_pipe.hpp>
#include <boost/asio/basic_stream_file.hpp>

namespace proc   = boost::process::v2;
namespace asio   = boost::asio;

std::tuple<int, std::string> execProc(const std::string& exePath, const std::vector<std::string>& args,
    const std::string& outputFilePath)
{
    asio::io_context ctx;

    asio::basic_stream_file<asio::any_io_executor> outStream(ctx, outputFilePath.c_str(), 
        asio::file_base::write_only | asio::file_base::create | asio::file_base::exclusive);

    asio::readable_pipe errPipe{ctx};

    proc::process proc{ ctx, exePath, args, proc::process_stdio{{}, outStream, errPipe}};

    std::string error;
    boost::system::error_code ec;
    auto errLen = asio::read(errPipe, asio::dynamic_buffer(error), ec);
    auto exitCode = proc.wait();
    outStream.close();

    return std::make_tuple(exitCode, std::move(error));
}

@klemens-morgenstern
Copy link
Collaborator

klemens-morgenstern commented Oct 24, 2024

Can you try the same without the exclusive flag?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants