By John Paul Mueller, Jeff Cogswell

Most programmers think of a stream as the same thing as a file. You know — a file that’s stored on your hard drive or maybe on a Universal Serial Bus (USB) flash drive or Secure Digital (SD) card. But streams go beyond just files. A stream is any type of data structure that you stream (that is, flow) your data into and out of in a sequence of bytes.

When you write an application that deals with files, you must use a specific order:

  1. Open the file.

    Before you can use a file, you must open it. In doing so, you specify a filename.

  2. Access the file.

    After you open a file, you either store data into it (this is called writing data to the file) or get data out of it (this is called reading data from the file).

  3. Close the file.

    After you have finished reading from and writing to a file, you must close the file.

For example, an application that tracks your stocks and writes your portfolio to a file at the end of the day might do these steps:

  1. Ask the user for a name of a file.

  2. Open the file.

  3. For each stock object, write the stock data to the file.

  4. Close the file.

The next morning, when the application starts, it might want to read the information back in. Here’s what it might do:

  1. Ask the user for the name of the file.

  2. Open the file.

  3. While there’s more data in the file, create a new Stock object, read the data from the file, and put the data into the Stock object.

  4. Close the file.

Here are a couple of reasons to close a file after you’ve finished using it:

  • Other applications might be waiting to use the file. Some operating systems allow an application to lock a file, meaning that no other applications can open the file while the application that locked the file is using it. In such situations, another application can use the file after you close it, but not until then.

  • When you write to a file, the operating system decides whether to immediately write the information onto the hard drive or flash drive/SD card or to hold on to it and gather more information, finally writing it all as a single batch. When you close a file, the operating system puts all your remaining data into the file. This is called flushing the file.

You have two ways to write to a file:

  • Sequential access: In sequential access, you write to a file or read from a file from beginning to end. With this approach, when you open the file, you normally specify whether you plan to read from or write to the file, but not both at the same time.

    After you open the file, if you’re writing to the file, the data you write gets added continually to the end of the file. Or if you’re reading from the file, you read the data at the beginning, then you read the data that follows, then you read the data that follows that data, and so on, up to the end.

  • Random access: With random access, you can read and write to any byte in a file, regardless of which byte you previously read or wrote. In other words, you can skip around. You can read some bytes, then move to another portion of the file and write some bytes, and then move elsewhere and write some more.

Back in the days of the C programming language, several library functions let you work with files. However, they stunk. They were cumbersome and made life difficult. And so, when C++ came along, people quickly created a set of classes that made life with files much easier. These people used the stream metaphor we’ve been raving about.