Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up with my own solution (which uses the same idea Stephen Chu suggested)</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cstdio&gt; #include &lt;QtCore&gt; using namespace std; void externalLibFunction(istream &amp; input_stream) { copy(istream_iterator&lt;string&gt;(input_stream), istream_iterator&lt;string&gt;(), ostream_iterator&lt;string&gt;(cout, " ")); } ifstream QFileToifstream(QFile &amp; file) { Q_ASSERT(file.isReadable()); return ifstream(::_fdopen(file.handle(), "r")); } int main(int argc, char ** argv) { QFile file("a file"); file.open(QIODevice::WriteOnly); file.write(QString("some string").toLatin1()); file.close(); file.open(QIODevice::ReadOnly); std::ifstream ifs(QFileToifstream(file)); externalLibFunction(ifs); } </code></pre> <p>Output:</p> <pre><code>some string </code></pre> <p>This code uses std::ifstream move constructor (C++x0 feature) specified in <em>27.9.1.7 basic_ifstream constructors</em> section of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf" rel="nofollow noreferrer"><strong>Working Draft, Standard for Programming Language C++</strong></a>:</p> <blockquote> <p><code>basic_ifstream(basic_ifstream&amp;&amp; rhs);</code><br> Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base class, and the contained basic_filebuf. Next basic_istream::set_rdbuf(&amp;sb) is called to install the contained basic_filebuf.</p> </blockquote> <p>See <a href="https://stackoverflow.com/questions/4825851/">How to return an fstream (C++0x)</a> for discussion on this subject.</p>
    singulars
    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