<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problem with QStream adding Extra Characters After a flush?]]></title><description><![CDATA[<p dir="auto">I have a work around but do not understand why I am experiencing this problem.</p>
<p dir="auto">I am using QT 5.6 on Ubuntu (not sure what version).</p>
<p dir="auto">I have the following code:</p>
<pre><code>    #include &lt;QCoreApplication&gt;
    #include &lt;QDebug&gt;
    #include &lt;QFile&gt;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    bool ok = false, second = false;
    double dr = 0.0;
    double dm = 0.0;
    QString tofile;
    QTextStream debug(&amp;tofile);
    QString line = "the cow jumped over the moon 100.0 -1.0 foo bar";
    QString buffer;
    QTextStream bufferstream(&amp; buffer);
    QStringList list = line.split(" ");

    foreach(buffer, list){
        debug&lt;&lt; "buffer before stream: " &lt;&lt; buffer &lt;&lt; '\n';
        buffer.toDouble( &amp;ok );
        if(ok == true &amp;&amp; second == true ){//found second valid double
            bufferstream &gt;&gt; dm;
            debug&lt;&lt; "buffer after stream and flush: "&lt;&lt; buffer &lt;&lt; '\n';
            break;
        }
        if(ok == true &amp;&amp; second == false){//found the first valid double
            second = true;
            bufferstream &gt;&gt; dr;
            debug&lt;&lt; "buffer first stream: " &lt;&lt; buffer &lt;&lt; '\n';
            bufferstream.flush();
        }
    }

    debug&lt;&lt; "dm: "&lt;&lt; dm &lt;&lt; "dr: "&lt;&lt; dr;

    qDebug()&lt;&lt; tofile;

    QFile file("StreamTestFile.txt");
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        qDebug()&lt;&lt;"error!";
    }

    QTextStream out(&amp;file);
    out &lt;&lt; tofile;
    file.close();

    return a.exec();
}
</code></pre>
<p dir="auto">After the second stream to extract the second double it adds characters, here is the output</p>
<p dir="auto">buffer before stream: the<br />
buffer before stream: cow<br />
buffer before stream: jumped<br />
buffer before stream: over<br />
buffer before stream: the<br />
buffer before stream: moon<br />
buffer before stream: 100.0<br />
buffer first stream: 100.0<br />
buffer before stream: -1.0<br />
buffer after stream and flush: -1.\00<br />
dm: 0dr: 100</p>
<p dir="auto">so you do not get the correct double.  Any explanation?  I fixed it by making new QTextStreams in each if statement...</p>
<p dir="auto">just tried this in Windows (minGW) and got the same thing.</p>
]]></description><link>https://forum.qt.io/topic/70862/problem-with-qstream-adding-extra-characters-after-a-flush</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 12:23:25 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/70862.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 02 Sep 2016 20:22:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem with QStream adding Extra Characters After a flush? on Sat, 03 Sep 2016 18:09:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a></p>
<p dir="auto">Yeah thanks I am aware of that method.</p>
<p dir="auto">Frankly have been reading a bit and 'discovered' the stream method to convert from QString to double and thought, 'Oh Cool let's do this!!" .</p>
<p dir="auto">Then I ran into the problem shown above.  I guess I am one of the few people crazy enough to try it.</p>
<p dir="auto">Still appreciate the feedback thanks.</p>
]]></description><link>https://forum.qt.io/post/345654</link><guid isPermaLink="true">https://forum.qt.io/post/345654</guid><dc:creator><![CDATA[ndt_mike]]></dc:creator><pubDate>Sat, 03 Sep 2016 18:09:05 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with QStream adding Extra Characters After a flush? on Sat, 03 Sep 2016 08:15:55 GMT]]></title><description><![CDATA[<p dir="auto">You're doing it wrong. Just use the stream to read from the string:</p>
<pre><code>QString line = "the cow jumped over the moon 100.0 -1.0 foo bar", word;
QTextStream in(&amp;line);
while (!in.atEnd())  {
    in &gt;&gt; word;

    bool ok;
    double value = word.toDouble(&amp;ok);
    if (!ok)
        continue;
     // ...
}
</code></pre>
]]></description><link>https://forum.qt.io/post/345605</link><guid isPermaLink="true">https://forum.qt.io/post/345605</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Sat, 03 Sep 2016 08:15:55 GMT</pubDate></item></channel></rss>