Skip to main content

Console applications (xtd.core)

xtd applications can use the xtd::console class to read characters from and write characters to the console. Data from the console is read from the standard input stream, data to the console is written to the standard output stream, and error data to the console is written to the standard error output stream. These streams are automatically associated with the console when the application starts and are presented as the in, out, and error properties, respectively.

The value of the console::in property is a std::istream object, whereas the values of the console::out and console::error properties are std::ostream objects. You can associate these properties with streams that do not represent the console, making it possible for you to point the stream to a different location for input or output. For example, you can redirect the output to a file by setting the console::out property to a std::ostream, which encapsulates a std::ofstream by means of the console::set_out method. The console::in and console::out properties do not need to refer to the same stream.

Note For more information about building console applications, including examples, see the documentation for the console class.

If the console does not exist, for example, in a xtd.forms application, output written to the standard output stream will not be visible, because there is no console to write the information to. Writing information to an inaccessible console does not cause an exception to be raised. (You can always change the application type to Console Application, for example, in the CMakeLists.txt file).

The xtd::console class has methods that can read individual characters or entire lines from the console. Other methods convert data and format strings, and then write the formatted strings to the console. For more information on formatting strings, see Formatting types.

Tips Console applications lack a message pump that starts by default.

See also