Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Global std_in

boost::process::std_in

Synopsis

// In header: <boost/process/io.hpp>

unspecified std_in;

Description

This property allows to set the input stream for the child process.

Details

File Input

The file I/O simple redirects the stream to a file, for which the possible types are

  • boost::process::filesystem::path

  • std::basic_string<char_type>

  • const char_type*

  • FILE*

with char_type being either char or wchar_t.

FILE* is explicitly added, so the process can easily redirect the output stream of the child to another output stream of the process. That is:

system("ls", std_in < stdin);
[Warning] Warning

If the launching and the child process use the input, this leads to undefined behaviour.

A syntax like system("ls", std_out > std::cerr) is not possible, due to the C++ implementation not providing access to the handle.

The valid expressions for this property are

std_in < file;
std_in = file;

Pipe Input

As explained in the corresponding section, the boost.process library provides a async_pipe class which can be used to communicate with child processes.

[Note] Note

Technically the async_pipe works synchronous here, since no asio implementation is used by the library here. The async-operation will then however not end if the process is finished, since the pipe remains open. You can use the async_close function with on_exit to fix that.

Valid expressions with pipes are these:

std_in < pipe;
std_in = pipe;

Where the valid types for pipe are the following:

Note that the pipe may also be used between several processes, like this:

pipe p;
child c1("nm", "a.out", std_out>p);
child c2("c++filt", std_in<p);

Asynchronous Pipe Input

Asynchronous Pipe I/O classifies communication which has automatically handling of the asynchronous operations by the process library. This means, that a pipe will be constructed, the async_read/-write will be automatically started, and that the end of the child process will also close the pipe.

Valid types for pipe I/O are the following:

  • boost::asio::const_buffer [29]

  • boost::asio::mutable_buffer [30]

  • boost::asio::streambuf

Valid expressions with pipes are these:

std_in < buffer;
std_in = buffer;
std_out > buffer;
std_out = buffer;
std_err > buffer;
std_err = buffer;
(std_out & std_err) > buffer;
(std_out & std_err) = buffer;
[Note] Note

It is also possible to get a future for std_in, by chaining another std::future<void> onto it, so you can wait for the input to be completed. It looks like this:

std::future<void> fut;
boost::asio::io_context ios;
std::string data;
child c("prog", std_in < buffer(data) >  fut, ios);
fut.get();

[Note] Note

boost::asio::buffer is also available in the boost::process namespace.

[Warning] Warning

This feature requires boost/process/async.hpp to be included and a reference to boost::asio::io_context to be passed to the launching function.

Close

The input stream can be closed, so it cannot be read from. This will lead to an error when attempted.

This can be achieved by the following syntax.

std_in < close;
std_in = close;
std_in.close();

Null

The input stream can be redirected to read from the null-device, which means that only EOF is read.

The syntax to achieve that has the following variants:

std_in < null;
std_in = null;
std_in.null();



[29] Constructed with boost::asio::buffer

[30] Constructed with boost::asio::buffer


PrevUpHomeNext