xtd 0.2.0
Loading...
Searching...
No Matches

◆ redirect_standard_input() [2/2]

process_start_info & xtd::diagnostics::process_start_info::redirect_standard_input ( bool  value)
noexcept

Sts a value indicating whether the input for an application is read from the Process.StandardInput stream.

Parameters
valuetrue if input should be read from Process.StandardInput; otherwise, false. The default is false.
Returns
The current instance of process_start_info.
Examples
The following example illustrates how to redirect the xtd::diagnostics::process::standard_input stream of a process. The example starts the sort command with redirected input. It then prompts the user for text, and passes that to the sort process by means of the redirected xtd::diagnostics::process::standard_input stream. The sort results are displayed to the user on the console.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::diagnostics;
using namespace xtd::io;
auto main() -> int {
console::write_line("Ready to sort one or more text lines...");
// Start the sort process with redirected input.
// Use the sort command to sort the input text.
block_scope_(process my_process) {
my_process.start_info().file_name("sort");
my_process.start_info().use_shell_execute(false);
my_process.start_info().redirect_standard_input(true);
my_process.start();
stream_writer my_stream_writer(my_process.standard_input());
// Prompt the user for input text lines to sort.
// Write each line to the StandardInput stream of
// the sort command.
string input_text;
int num_lines = 0;
do {
console::write_line("Enter a line of text (or press the Enter key to stop):");
input_text = console::read_line();
if (input_text.size() > 0) {
num_lines++;
my_stream_writer.write_line(input_text);
}
} while (input_text.size() > 0);
// Write a report header to the console.
if (num_lines > 0) {
console::write_line(" {} sorted text line(s) ", num_lines);
console::write_line("------------------------");
} else {
console::write_line(" No input was sorted");
}
// End the input stream to the sort command.
// When the stream closes, the sort command
// writes the sorted text lines to the
// console.
my_stream_writer.close();
// Wait for the sort process to write the sorted text lines.
my_process.wait_for_exit();
}
}
size_type size() const noexcept
Returns the number of char_t elements in the string, i.e. std::distance(begin(), end()).
Definition basic_string.h:834
static xtd::string read_line()
Reads the next line of characters from the standard input stream.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
bool redirect_standard_input() const noexcept
Gets a value indicating whether the input for an application is read from the Process....
const xtd::string & file_name() const noexcept
Gets the application or document to start.
bool use_shell_execute() const noexcept
Gets a value indicating whether to use the operating system shell to start the process.
const xtd::diagnostics::process_start_info & start_info() const
Gets the properties to pass to the xtd::diagnostics::process::start() method of the xtd::diagnostics:...
std::ostream & standard_input()
Gets a stream used to write the input of the application.
bool start()
Starts (or reuses) the process resource that is specified by the xtd::diagnostics::process::start_inf...
process & wait_for_exit()
Instructs the xtd::diagnostics::process component to wait indefinitely for the associated process to ...
Provides access to local and remote processes and enables you to start and stop local system processe...
Definition process.h:49
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.h:28
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
The xtd::diagnostics namespace provides classes that allow you to interact with system processes,...
Definition assert_dialog_result.h:10
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
A xtd::diagnostics::process can read input text from its standard input stream, typically the keyboard. By redirecting the xtd::diagnostics::process::standard_input stream, you can programmatically specify the input. For example, instead of using keyboard input, you can provide text from the contents of a designated file or output from another application.
Note
To use xtd::diagnostics::process::standard_input, you must set xtd::diagnostics::process_start_info::use_shell_execute to false, and you must set xtd::diagnostics::process_start_info::redirect_standard_input to true. Otherwise, writing to the xtd::diagnostics::process::standard_input stream throws an exception.