Concepts / Formatted and Unformatted Output

Formatted and Unformatted Output

The I/O stream library provides formatted and unformatted buffered I/O of text and numeric values.

  • Programming

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?

  • It is sent unchanged as a typed integer

  • It is converted into a stream of characters, which the source describes as bytes

  • It is converted into a fraction first

Reveal answer
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.

Typed objecta whole number such as 42ostreamconverts the objectStream of charactersdescribed as bytesDestinationscreen, file, or logsent toproducesflows to
What happens step by step when a typed number is sent to an ostream, and how it becomes a sequence of characters on its way to the destination?

The Ostream and Its Conversion Step

Definition

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.

Key point

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.

Worked example

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.

  1. 1
    Start with a typed object

    The program holds 42 as a typed value. It understands the value by its type, which is a whole number.

  2. 2
    Pass 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.

  3. 3
    Produce 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.

  4. 4
    Hand 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.

Result

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.

Programholds typed valuesBufferholding area for waitingdataOutput devicescreen, file, or logdata moves todata is written to
Where does the data sit between the program and the screen or file, and how does it move through that holding area?

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.

AspectWhat the overview states
Styles offeredFormatted and unformatted I/O, both part of the same family of facilities
Shared propertyBuffering applies alongside both styles
Kinds of valuesText values and numeric values
Detailed differences between the stylesNot 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 like

    Assuming that 42 leaves the program still as a typed whole number.

    Why it fails

    The ostream converts typed objects into a stream of characters, which the source describes as bytes.

    Do this instead

    Describe 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 like

    Assuming that a whole number and a word each need their own output method.

    Why it fails

    The 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 instead

    Think 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 like

    Assuming that only the formatted style is buffered.

    Why it fails

    The overview attaches buffering to the library's I/O as a whole, alongside both styles.

    Do this instead

    Read 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 like

    Stating exactly when buffered data is written out, or exactly how each style changes the data.

    Why it fails

    The overview leaves the detailed behavior of both styles and the buffer for later study.

    Do this instead

    State only what the overview claims, and mark the detailed behavior as something to study next.

Trace It Yourself

EASY

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.
MEDIUM

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.

Related concepts

More concepts starting with F