Formatted and Unformatted Output
The I/O stream library provides formatted and unformatted buffered I/O of text and numeric values.
A Value Leaving the Program
A program keeps its information in typed values: a whole number, a fraction, a word, a single letter. While the program runs, those values live in memory. A person reading a screen, a file, or a log cannot read them directly, so something has to carry each value from the program out to a destination.
That something must handle many different kinds of values through one consistent route. The I/O stream library is the part of the language that provides this route. As you read, keep one question in mind: when a value leaves the program, what form does it take on the way out? Working through that question is the point of this article.
Following a Value Out of the Program
Take a single whole number, 42, that the program holds as a typed object. Before you read the answer, try to predict its form once it reaches the output side.
What do you think happens?
When the typed number 42 is sent toward the output, what form does it take on the way out?
Reveal answer
It is converted into a stream of characters, which the source describes as bytes.
The key component on the output side is the ostream. It takes a typed object, which the program understands by its type, and produces a sequence of characters that can flow onward to a destination.
The Ostream and Its Conversion Step
An ostream is the output-side component of the I/O stream library. Its defining behavior is that it converts typed objects into a stream of characters, which the source describes as bytes. A typed object might be a number or a piece of text.
The conversion step is what makes the design useful. The program does not need a separate output routine for every type it might print. Each typed object goes through the same conversion step, and what comes out the other side is always a stream of characters. The rest of the pipeline only has to deal with characters, so one output route can serve many different kinds of typed values.
The object goes in typed, and a stream of characters comes out. If you can describe that change in your own words, you have the core of how an ostream works.
Sending a Whole Number Out
A program holds the whole number 42 in a typed value and needs to send it toward a screen. Trace what happens to it, using only the overview's account.
- 1Start with a typed object
The program holds 42 as a typed value. It understands the value by its type, which is a whole number.
- 2Pass it through the ostream
The ostream takes the typed object and converts it. The conversion does not depend on a special routine written for whole numbers.
- 3Produce a stream of characters
What comes out is a sequence of characters, which the source describes as bytes. The value is now in a form that can flow onward.
- 4Hand the characters onward
The characters flow toward a destination such as a screen, a file, or a log. The downstream part of the pipeline only has to handle characters.
The typed whole number 42 leaves the program as a stream of characters, which is the form the rest of the pipeline handles.
Where Data Waits: The Buffer
The overview describes the library's I/O as buffered. In general terms, a buffer is a holding area where data waits while it is moved between the program and its destination. The buffer sits between the program and the output device, as shown below.
Two Styles and Two Kinds of Values
The overview names three words that describe the library's I/O: formatted and unformatted name two styles, buffered names a property of how data is handled as it moves, and text and numeric values name what the library works with. The table below summarizes what the overview claims and what it leaves out.
| Aspect | What the overview states |
|---|---|
| Styles offered | Formatted and unformatted I/O, both part of the same family of facilities |
| Shared property | Buffering applies alongside both styles |
| Kinds of values | Text values and numeric values |
| Detailed differences between the styles | Not covered in this overview; left for later study |
What the overview claims about the library's I/O
Mistakes When Reading the Overview
Common mistake Believing the ostream sends a typed number out unchanged
What it looks likeAssuming that 42 leaves the program still as a typed whole number.
Why it failsThe ostream converts typed objects into a stream of characters, which the source describes as bytes.
Do this insteadDescribe the output as a conversion: typed object in, stream of characters out.
Common mistake Writing a separate output routine for every type
What it looks likeAssuming that a whole number and a word each need their own output method.
Why it failsThe conversion step is shared, so each typed object goes through the same process and the rest of the pipeline only deals with characters.
Do this insteadThink of one route that accepts many typed values and produces characters.
Common mistake Treating buffering as a feature of only one style
What it looks likeAssuming that only the formatted style is buffered.
Why it failsThe overview attaches buffering to the library's I/O as a whole, alongside both styles.
Do this insteadRead buffering as a property that applies to both formatted and unformatted I/O.
Common mistake Inventing detailed rules the overview does not give
What it looks likeStating exactly when buffered data is written out, or exactly how each style changes the data.
Why it failsThe overview leaves the detailed behavior of both styles and the buffer for later study.
Do this insteadState only what the overview claims, and mark the detailed behavior as something to study next.
Trace It Yourself
A program holds the word hello and a fraction such as 3.5 as typed values. Trace each value from the program to the destination. For each one, name the typed object, the component that converts it, the form it takes on the way out, and the component it passes through before reaching the output device.
Hints
- The component that converts typed objects is the ostream.
- The form each value takes after conversion is a stream of characters, described as bytes.
- The buffer is the holding area between the program and the output device.
Explain in two or three sentences why one output route can handle both a word and a number, even though they are different kinds of typed values.
Hints
- Focus on what the ostream produces, not on what the value was before it arrived.
- The rest of the pipeline only has to handle characters.
Key Takeaways
- The I/O stream library provides the route that carries typed values from a program to a destination such as a screen, file, or log.
- An ostream converts typed objects, whether numbers or text, into a stream of characters, which the source describes as bytes.
- Because every typed object goes through the same conversion, one output route serves many kinds of values, and the rest of the pipeline only handles characters.
- Buffering is a holding area property of the library's I/O, and it applies alongside both the formatted and unformatted styles.
- The overview names formatted and unformatted styles but leaves their detailed behavior and buffering timing for later study.