Online Book Reader

Home Category

Professional C__ - Marc Gregoire [265]

By Root 1248 0
Don’t worry though; after a few examples, you’ll never look back.

What Is a Stream, Anyway?

Chapter 1 compares the cout stream like a laundry chute for data. You throw some variables down the stream, and they are written to the user’s screen, or console. More generally, all streams can be viewed as data chutes. Streams vary in their direction and their associated source or destination. For example, the cout stream that you are already familiar with is an output stream, so its direction is “out.” It writes data to the console so its associated destination is “console.” There is another standard stream called cin that accepts input from the user. Its direction is “in,” and its associated source is “console.” Both cout and cin are predefined instances of streams that are defined within the std namespace in C++. The following table gives a brief description of all predefined streams. The difference between buffered and unbuffered streams is explained in a later section:

STREAM DESCRIPTION

cin An input stream, reads data from the “input console.”

cout A buffered output stream, writes data to the “output console.”

cerr An unbuffered output stream, writes data to the “error console,” which is often the same as the “output console.”

clog A buffered version of cerr.

Note that graphical user interface applications normally do not have a console; i.e., if you write something to cout, the user will not see it. If you are writing a library, you should never assume the existence of cout, cin, cerr or clog because you never know if your library will be used in a console or in a GUI application.

Every input stream has an associated source. Every output stream has an associated destination.

Another important aspect of streams is that they include data but also have a so-called current position. The current position is the position in the stream where the next read or write operation will take place.

Stream Sources and Destinations

Streams as a concept can be applied to any object that accepts data or emits data. You could write a stream-based network class or stream-based access to a MIDI-based instrument. In C++, there are three common sources and destinations for streams.

You have already read many examples of user, or console, streams. Console input streams make programs interactive by allowing input from the user during run time. Console output streams provide feedback to the user and output results.

File streams, as the name implies, read data from a file system and write data to a file system. File input streams are useful for reading in configuration data and saved files or for batch processing file-based data. File output streams are useful for saving state and providing output. File streams subsume the functionality of the C functions fprintf(), fwrite(), and fputs()for output, and fscanf(), fread(), and fgets() for input.

String streams are an application of the stream metaphor to the string type. With a string stream, you can treat character data just as you would treat any other stream. For the most part, this is merely a handy syntax for functionality that could be handled through methods on the string class. However, using stream syntax provides opportunities for optimization and can be far more convenient than direct use of the string class. String streams subsume the functionality of sprintf(), sprintf_s(), and other forms of C string formatting functions.

The rest of this section deals with console streams (cin and cout). Examples of file and string streams are provided later in this chapter. Other types of streams, such as printer output or network I/O are often platform dependent, so they are not covered in this book.

Output with Streams

Output using streams is introduced in Chapter 1 and is used in almost every chapter in this book. This section will briefly revisit some of the basics and will introduce material that is more advanced.

Output Basics

Output streams are defined in the header file. Most programmers include in their programs, which in turn includes the headers for

Return Main Page Previous Page Next Page

®Online Book Reader