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. I have a Doubt with File Streams
Qt 6.11 is out! See what's new in the release blog

I have a Doubt with File Streams

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.1k 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.
  • L Offline
    L Offline
    lucho2013
    wrote on last edited by
    #1

    I'm learning Qt reading a book, and I have installed Qt creator on Linux. This is my first example than probe in Qt.
    I have this code Qt about file streams:

    @//#include <QCoreApplication>
    #include <QTextStream>
    #include <QString>
    #include <QFile>

    QTextStream cout (stdout);
    QTextStream cerr (stderr);

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

    QString str, newstr;
    QTextStream strbuf(&str);
    
    int lucky = 7;
    float pi = 3.14;
    double e = 2.71;
    
    cout << "An in-memory stream" << endl;
    strbuf << "pi : "<< pi << endl
           << "luckynumber: " << lucky<< endl
           << " e : " << e << endl;
    
    cout << str;
    
    QFile data("mydata");
    data.open(QIODevice::WriteOnly);
    QTextStream out(&data);
    out << str ;
    data.close();
    
    cout << "Read data from the file - watch for errors. " << endl;
    if (data.open(QIODevice::ReadOnly)) {
        QTextStream in(&data);
        float pi2;
        in >> newstr >> pi2;// *it seems Problem in this point- NOTE_1*
        if (pi2 != pi)
            cerr << "ERROR! wrong " << newstr << pi2 << endl;
        else
            cout << newstr << "OK" << endl;
    
        int lucky2;
        in >> newstr >> lucky2;
    

    /* if (pi2 != pi)
    cerr << "ERROR! Wrong " << newstr << pi2 << endl;
    else*/
    cout << newstr << lucky2 << endl;//" OK" << endl;

        double e2;
        in >> newstr >> e2;
        if (e2 != e)
            cerr << "ERROR! Wrong " << newstr << e2 << endl;
        else
            cout << newstr << " OK" << endl;
    
        data.close();
    }
    
    cout << "Read from file line-by-line" << endl;
    if (data.open(QIODevice::ReadOnly)){
        QTextStream in(&data);
        while (not in.atEnd()) {
            newstr = in.readLine();
            cout << newstr << endl;
        }
        data.close();
    }
    return 0;//a.exec&#40;&#41;;
    

    }
    @

    and when i save, compile and run, get it:

    An in-memory stream
    pi : 3.14
    luckynumber : 7
    e : 2.71
    Read data from the file - watch from errors.
    ERROR! wrong pi0
    :3
    ERROR! wrong .140
    Read from file line-by-line
    pi : 3.14
    luckynumber : 7
    e : 2.71
    Press <RETURN> to close this window...

    NOTE_1: my question is: Why the value of pi can't be copied in pi2?

    Thanks in advance

    Lucho

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      this just can't work.
      You write a single string to the file. But later you try to read a string and a float.

      writing:
      @
      QTextStream out(&data);
      out << str ;
      data.close();
      @

      reading:
      @
      QTextStream in(&data);
      float pi2;
      in >> newstr >> pi2;
      @

      And don't use QTextStream then, instead use QDataStream (since you obviously want to stream other types than text only).

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tomma
        wrote on last edited by
        #3

        From QTextStream doc:

        bq. There are three general ways to use QTextStream when reading text files:

        • Chunk by chunk, by calling readLine() or readAll().
        • Word by word. QTextStream supports streaming into QStrings, QByteArrays and char* buffers. Words are delimited by space, and leading white space is automatically skipped.
        • Character by character, by streaming into QChar or char types. This method is often used for convenient input handling when parsing files, independent of character encoding and end-of-line semantics. To skip white space, call skipWhiteSpace().

        Which means that you should remove extra white spaces or serialize data somehow.

        1 Reply Last reply
        0

        • Login

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