I have a Doubt with File Streams

  • 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");
    QTextStream out(&data);
    out << str ;
    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;
            cout << newstr << "OK" << endl;
        int lucky2;
        in >> newstr >> lucky2;

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

        double e2;
        in >> newstr >> e2;
        if (e2 != e)
            cerr << "ERROR! Wrong " << newstr << e2 << endl;
            cout << newstr << " OK" << endl;
    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;
    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
    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


  • Moderators

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

    QTextStream out(&data);
    out << str ;

    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).

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.