[ fromfile: filestreams.xml id: filestreams ]
Streams are used for reading from or writing to files, network connections, and also strings.
Example 1.11 creates some strings from characters and numerics and writes them to a file.
Example 1.11. src/stl/streams/streams.cpp
[ . . . . ] #include <iostream> #include <sstream> #include <fstream> int main() { using namespace std; ostringstream strbuf; int lucky = 7; float pi=3.14; double e=2.71; cout << "An in-memory stream" << endl; strbuf << "luckynumber: " << lucky << endl << "pi: " << pi << endl << "e: " << e << endl; string strval = strbuf.str(); cout << strval; ofstream outf; outf.open("mydata"); outf << strval ; outf.close();
You can use simple input operators to read from the file and, because there is whitespace between records, the insertion operator might look like Example 1.12
Example 1.12. src/stl/streams/streams.cpp
[ . . . . ] cout << "Read data from the file - watch for errors." << endl; string newstr; ifstream inf; inf.open("mydata"); if(inf) { int lucky2; inf >> newstr >> lucky2; if (lucky != lucky2) cerr << "ERROR! wrong " << newstr << lucky2 << endl; else cout << newstr << " OK" << endl; float pi2; inf >> newstr >> pi2; if (pi2 != pi) cerr << "ERROR! Wrong " << newstr << pi2 << endl; else cout << newstr << " OK" << endl; double e2; inf >> newstr >> e2; if (e2 != e) cerr << "ERROR: Wrong " << newstr << e2 << endl; else cout << newstr << " OK" << endl; inf.close(); }
You can read files line-by-line
Example 1.13. src/stl/streams/streams.cpp
[ . . . . ] cout << "Read from file line-by-line" << endl; inf.open("mydata"); if(inf) { while (not inf.eof()) { getline(inf, newstr); cout << newstr << endl; } inf.close(); } return 0; }
Example 1.14 does the same things using Qt files, strings, and streams.
Example 1.14. src/qtstreams/files/qdemo.cpp
#include <QTextStream> #include <QString> #include <QFile> QTextStream cout(stdout); QTextStream cerr(stderr); int main() { QString str, newstr; QTextStream strbuf(&str); int lucky = 7; float pi = 3.14; double e = 2.71; cout << "An in-memory stream" << endl; strbuf << "luckynumber: " << lucky << endl << "pi: " << pi << 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); int lucky2; in >> newstr >> lucky2; if (lucky != lucky2) cerr << "ERROR! wrong " << newstr << lucky2 << endl; else cout << newstr << " OK" << endl; float pi2; in >> newstr >> pi2; if (pi2 != pi) cerr << "ERROR! Wrong " << newstr << pi2 << endl; else cout << newstr << " 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; }
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |