Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

I have a Doubt with File Streams

$
0
0
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(); } 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

Viewing all articles
Browse latest Browse all 13965