1.9.  Streams

[ fromfile: streams.xml id: streams ]

Streams are objects used for reading and writing. The Standard Library defines <iostream>. Qt defines <QTextStream> for the equivalent functionality.

You already saw before that iostream defines the three global streams:

Also defined in <iostream> are manipulators, such as flush and endl. Manipulators are implicit calls to functions that can change the state of a stream object in various ways. A manipulator can be added to

Example 1.9 demonstrates the use of manipulators applied to the console output stream.

Example 1.9. src/stdstreams/streamdemo.cpp

#include <iostream>

int main() {
    using namespace std;
    int num1(1234), num2(2345) ;
    cout << oct << num2 << '\t'
            << hex << num2 << '\t'
             << dec << num2 
             << endl;
    cout << (num1 < num2) << endl;
    cout << boolalpha
             << (num1 < num2)
             << endl;
    double dub(1357);
    cout << dub << '\t'
            << showpos << dub << '\t'
            << showpoint << dub 
            << endl;
    dub = 1234.5678;
    cout << dub << '\t'
            << fixed << dub << '\t'
            << scientific << dub << '\n'
            << noshowpos << dub 
            << endl;
}
Output:

4451    929     2345
1
true
1357    +1357   +1357.00
+1234.57        +1234.567800    +1.234568e+03   
1.234568e+03


             

<include src="src/stdstreams/streamdemo.cpp" href="src/stdstreams/streamdemo.cpp" id="streamdemo1" mode="cpp"/>


It is easy to define QTextStreams with the same names as their equivalent iostream counterparts. Because console input and output streams are often used primarily for debugging purposes, Qt provides a global function, qDebug() that facilitates sending messages to the console (whatever that may be) with a flexible interface demonstrated in Example 1.10.

Example 1.10. src/qtstreams/qtstreamdemo.cpp

#include <QTextStream>
#include <QDebug>

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

int main() {
   int num1(1234), num2(2345) ;
    cout << oct << num2 << '\t'
            << hex << num2 << '\t'
             << dec << num2 
             << endl;
    double dub(1357);
    cout << dub << '\t'
            << forcesign << dub << '\t'
            << forcepoint << dub 
            << endl;
    dub = 1234.5678;
    cout << dub << '\t'
            << fixed << dub << '\t'
            << scientific << dub << '\n'
            << noforcesign << dub 
            << endl;
    qDebug() << "Here is a debug message with " << dub << "in it." ;
    qDebug("Here is one with the number %d in it.", num1 );
}
Output:

4451    929     2345
1357    +1357   +1357.00
+1234.57        +1234.567800    +1.234568e+03
1.234568e+03
Here is a debug message with  1234.57 in it.
Here is one with the number 1234 in it.


<include src="src/qtstreams/qtstreamdemo.cpp" href="src/qtstreams/qtstreamdemo.cpp" id="qtstreamdemo1" mode="cpp"/>


The symbols stdin, stdout, and stderr come from the C standard library. Note that QTextStream also provides manipulators, some of which are spelled the same as the ones used in Example 1.9 with iostream.