Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QStream adding Extra Characters After a flush?
Forum Updated to NodeBB v4.3 + New Features

Problem with QStream adding Extra Characters After a flush?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 838 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ndt_mike
    wrote on 2 Sept 2016, 20:22 last edited by
    #1

    I have a work around but do not understand why I am experiencing this problem.

    I am using QT 5.6 on Ubuntu (not sure what version).

    I have the following code:

        #include <QCoreApplication>
        #include <QDebug>
        #include <QFile>
    
    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(&tofile);
        QString line = "the cow jumped over the moon 100.0 -1.0 foo bar";
        QString buffer;
        QTextStream bufferstream(& buffer);
        QStringList list = line.split(" ");
    
        foreach(buffer, list){
            debug<< "buffer before stream: " << buffer << '\n';
            buffer.toDouble( &ok );
            if(ok == true && second == true ){//found second valid double
                bufferstream >> dm;
                debug<< "buffer after stream and flush: "<< buffer << '\n';
                break;
            }
            if(ok == true && second == false){//found the first valid double
                second = true;
                bufferstream >> dr;
                debug<< "buffer first stream: " << buffer << '\n';
                bufferstream.flush();
            }
        }
    
        debug<< "dm: "<< dm << "dr: "<< dr;
    
        qDebug()<< tofile;
    
        QFile file("StreamTestFile.txt");
        if (!file.open(QFile::WriteOnly | QFile::Text)) {
            qDebug()<<"error!";
        }
    
        QTextStream out(&file);
        out << tofile;
        file.close();
    
        return a.exec();
    }
    

    After the second stream to extract the second double it adds characters, here is the output

    buffer before stream: the
    buffer before stream: cow
    buffer before stream: jumped
    buffer before stream: over
    buffer before stream: the
    buffer before stream: moon
    buffer before stream: 100.0
    buffer first stream: 100.0
    buffer before stream: -1.0
    buffer after stream and flush: -1.\00
    dm: 0dr: 100

    so you do not get the correct double. Any explanation? I fixed it by making new QTextStreams in each if statement...

    just tried this in Windows (minGW) and got the same thing.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 3 Sept 2016, 08:15 last edited by
      #2

      You're doing it wrong. Just use the stream to read from the string:

      QString line = "the cow jumped over the moon 100.0 -1.0 foo bar", word;
      QTextStream in(&line);
      while (!in.atEnd())  {
          in >> word;
      
          bool ok;
          double value = word.toDouble(&ok);
          if (!ok)
              continue;
           // ...
      }
      

      Read and abide by the Qt Code of Conduct

      N 1 Reply Last reply 3 Sept 2016, 18:09
      0
      • K kshegunov
        3 Sept 2016, 08:15

        You're doing it wrong. Just use the stream to read from the string:

        QString line = "the cow jumped over the moon 100.0 -1.0 foo bar", word;
        QTextStream in(&line);
        while (!in.atEnd())  {
            in >> word;
        
            bool ok;
            double value = word.toDouble(&ok);
            if (!ok)
                continue;
             // ...
        }
        
        N Offline
        N Offline
        ndt_mike
        wrote on 3 Sept 2016, 18:09 last edited by
        #3

        @kshegunov

        Yeah thanks I am aware of that method.

        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!!" .

        Then I ran into the problem shown above. I guess I am one of the few people crazy enough to try it.

        Still appreciate the feedback thanks.

        1 Reply Last reply
        0

        1/3

        2 Sept 2016, 20:22

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved