Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using C's <a href="http://linux.die.net/man/3/popen" rel="nofollow noreferrer"><code>popen</code></a> (probably the simplest solution, even if it doesn't use C++'s <code>iostream</code>):</p> <pre><code>FILE *p = popen("echo hello", "r"); std::string s; for (size_t count; (count = fread(buf, 1, sizeof(buf), p));) s += string(buf, buf + count); pclose(p); </code></pre> <p>Assuming your <code>iostream</code> has the non-standard <em>x</em><code>fstream::</code><em>x</em><code>fstream(int fd)</code> constructor:</p> <pre><code>FILE *p = popen("echo hello", "r"); std::ifstream p2(fileno(p)); std::string s; p2 &gt;&gt; s; p2.close(); pclose(p); </code></pre> <p>Using <a href="http://www.boost.org/doc/libs/release/libs/iostreams/" rel="nofollow noreferrer">Boost.Iostreams</a>, you don't have to depend upon non-standard extensions to <code>iostream</code>:</p> <pre><code>boost::iostreams::file_descriptor_source p2(fileno(p)); </code></pre> <p>Unfortunately, Windows is horrible and <a href="http://msdn.microsoft.com/en-us/library/96ayss4b.aspx" rel="nofollow noreferrer"><code>_popen</code></a> only works for console applications; for a graphical app:</p> <pre><code>SECURITY_ATTRIBUTES sec; sec.nLength = sizeof(sec); sec.bInheritHandle = TRUE; sec.lpSecurityDescriptor = NULL; HANDLE *h[2]; CreatePipe(&amp;h[0], &amp;h[1], &amp;sec, 0); SetHandleInformation(h[0], HANDLE_FLAG_INHERIT, 0) STARTUPINFO si; memset((void *)&amp;si, 0, sizeof(si)); si.hStdInput = INVALID_HANDLE_VALUE; si.hStdOutput = h[1]; si.hStdError = INVALUD_HANDLE_VALUE; si.dwFlags |= STARTF_USESTDHANDLES; CreateProcess(NULL, "cmd /c \"echo hello\"", NULL, NULL, TRUE, 0, NULL, NULL, &amp;si, NULL); boost::iostreams::file_descriptor_source p(h[0]); </code></pre> <p>(completely untested)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload