Streams are objects used for reading and writing. The Standard Library defines , while Qt defines <QTextStream> for the equivalent functionality.
iostream defines the three global streams:
Also defined (in both and ) are manipulators, such as flush and endl. A manipulator can be added to
The code in Example 1.15 demonstrates the use of several manipulators applied to the standard output stream.
Example 1.15. src/stdstreams/streamdemo.cpp
#include int main() { using namespace std; int num1(1234), num2(2345) ; cout << oct << num2 << ' ' << hex << num2 << ' ' << dec << num2 << endl; cout << (num1 < num2) << endl; cout << boolalpha << (num1 < num2) << endl; double dub(1357); cout << dub << ' ' << showpos << dub << ' ' << showpoint << dub << endl; dub = 1234.5678; cout << dub << ' ' << fixed << dub << ' ' << scientific << dub << ' ' << noshowpos << dub << endl; } Output: 4451 929 2345 1 true 1357 +1357 +1357.00 +1234.57 +1234.567800 +1.234568e+03 1.234568e+03 |
Streams are used for reading from or writing to files, network connections, and also strings. One useful feature of streams is that they make it easy to produce strings from mixed types of data. In Example 1.16, we will create some strings from numerics and write them to a file.
Example 1.16. src/stl/streams/streams.cpp
[ . . . . ] int main() { using namespace std; ostringstream strbuf; int lucky = 7; float pi=3.14; double e=2.71; /* An in-memory stream */ strbuf << "luckynumber " << lucky << endl << "pi " << pi << endl << "e " << e << endl; string strval = strbuf.str(); <-- 1 cout << strval; /* An output file stream. */ ofstream outf; outf.open("mydata"); <-- 2 outf << strval ; outf.close();
|
After the strings have been written, we have a couple of choices for how to read them. We can use the analogous input operators that we wrote to output, and because there is whitespace between each record, the insertion operator will work as shown in Example 1.17.
Example 1.17. src/stl/streams/streams.cpp
[ . . . . ] /* An input file stream */ ifstream inf; inf.open("mydata"); string newstr; int lucky2; inf >> newstr >> lucky2; if (lucky != lucky2) cerr << "ERROR! wrong lucky number" << endl; float pi2; inf >> newstr >> pi2; if (pi2 != pi) cerr << "ERROR! Wrong pi." << endl; double e2; inf >> newstr >> e2; if (e2 != e) cerr << "e2: " << e2 << " e: " << e << endl; inf.close(); |
In addition, we can read files line-by-line and deal with each line as a string, as shown in Example 1.18.
Example 1.18. src/stl/streams/streams.cpp
[ . . . . ] /* Read line-by-line */ inf.open("mydata"); while(not inf.eof()) { getline(inf, newstr); cout << newstr << endl; } inf.close(); return 0; } |
Exercise: Streams
Modify the program in Example 1.16 so that it does the following:
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments