io.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #include <boost/process.hpp>
  10. #include <string>
  11. namespace bp = boost::process;
  12. int main()
  13. {
  14. //
  15. bp::system(
  16. "test.exe",
  17. bp::std_out > stdout, //forward
  18. bp::std_err.close(), //close
  19. bp::std_in < bp::null //null in
  20. );
  21. boost::filesystem::path p = "input.txt";
  22. bp::system(
  23. "test.exe",
  24. (bp::std_out & bp::std_err) > "output.txt", //redirect both to one file
  25. bp::std_in < p //read input from file
  26. );
  27. {
  28. bp::opstream p1;
  29. bp::ipstream p2;
  30. bp::system(
  31. "test.exe",
  32. bp::std_out > p2,
  33. bp::std_in < p1
  34. );
  35. p1 << "my_text";
  36. int i = 0;
  37. p2 >> i;
  38. }
  39. {
  40. boost::asio::io_context io_context;
  41. bp::async_pipe p1(io_context);
  42. bp::async_pipe p2(io_context);
  43. bp::system(
  44. "test.exe",
  45. bp::std_out > p2,
  46. bp::std_in < p1,
  47. io_context,
  48. bp::on_exit([&](int exit, const std::error_code& ec_in)
  49. {
  50. p1.async_close();
  51. p2.async_close();
  52. })
  53. );
  54. std::vector<char> in_buf;
  55. std::string value = "my_string";
  56. boost::asio::async_write(p1, boost::asio::buffer(value), []( const boost::system::error_code&, std::size_t){});
  57. boost::asio::async_read (p2, boost::asio::buffer(in_buf), []( const boost::system::error_code&, std::size_t){});
  58. }
  59. {
  60. boost::asio::io_context io_context;
  61. std::vector<char> in_buf;
  62. std::string value = "my_string";
  63. bp::system(
  64. "test.exe",
  65. bp::std_out > bp::buffer(in_buf),
  66. bp::std_in < bp::buffer(value)
  67. );
  68. }
  69. {
  70. boost::asio::io_context io_context;
  71. std::future<std::vector<char>> in_buf;
  72. std::future<void> write_fut;
  73. std::string value = "my_string";
  74. bp::system(
  75. "test.exe",
  76. bp::std_out > in_buf,
  77. bp::std_in < bp::buffer(value) > write_fut
  78. );
  79. write_fut.get();
  80. in_buf.get();
  81. }
  82. }