Skip to main content

Section 11.4 Files and Streams

Subsection 11.4.1 Files

Programs need ways to store information in a way that is accessible to other programs and that will persist after the program ends. The most common way to do this is to write information to a file.
Files come in two main flavors, binary and text based. In reality, all files are binary - they are all just a sequence of 1s and 0s. But in text based files, all of the content is ASCII encoded (or Unicode or some similar encoding) text. Text files are designed to be easy for humans to open and read. All of our source code files in C++ are written as text files. They also are often used for things like configuration files, log files, etc...
In a binary file, there may be some text that is encoded as characters, but most of the content is 1s and 0s that have other meanings - numbers, colors, etc... Binary files are typically more compact and quicker for computers to read and write. But they are much more difficult for humans to work with - you need a special program to edit the raw data and knowledge of how the data has been structured. Binary files are typically used for things like images, movies, complex documents that include formatting (like MS Word documents), and storage of large amounts of data.
In this book we will focus on text files. They are much easier to work with and are perfectly good for learning the basics of reading and writing data. There are some general tricks for working with binary data, but most of the challenges when working with binary files relate to understanding how a particular file is structured rather than how to read/write the data.
We will refer to the files in this book that your programs can work with as datafiles. Here is a datafile named Numbers.txt:
Data: Numbers.txt
10 20 30
40 50 60
In a program we can refer to "Numbers.txt" to specify that file.

Warning 11.4.1.

You will only be able to use datafiles in programs that are set up to include them. Make sure to check the instructions for any exercises as to what datafiles are available for use in that program.

Note 11.4.2. The working directory.

The working directory refers to the directory on your computer’s filesystem where a program is doing its work. Often times for the program we write, the working directory is simply the same directory as the program is located (something like C:\Programs\HelloWorld or \home\astudent\HelloWorld). But it is possible to be currently working in one directory and launch a program that is somewhere else.
In this book, datafiles that you are to read will automatically be placed in the working directory. If you are running a program on your own computer, you will have to make sure that you know where your working directory is and you place any files you want to work with in it.

Checkpoint 11.4.1.

Subsection 11.4.2 Streams

The tools we will use to work with files in C++ are called ofstream and ifstream. The stream part of the name refers to a general idea - a β€œstream” of data that is produced by one thing and consumed by another.
We have already encountered some streams. cin and cout are streams that send data between the console and a program. cin is an input stream, it bring information from the console to the program. cout on the other hand is an output stream that sends data from the program to the console. Each time we use >> or << we are either removing some data from a stream or inserting some data into it.
As you will see, working with files is not that different than working with the console. That is the power of an abstraction. By thinking of both working with files and the console as examples of β€œsending information to/from the program”, we can develop and use one set of techniques that lets us solve both problems.

Checkpoint 11.4.2.

What is a stream object?
  • an abstract object that works exclusively with cin and cout statements
  • Incorrect! Stream objects do work with cin and cout, but that is not all that they do!
  • an abstract object on which input and ouput operations are performed
  • Correct!
  • an abstract object that works only with file data
  • Incorrect! Stream objects do work with file data, but they do other things too.
  • an abstract object that controls the flow of statements
  • Incorrect! This is not at all what stream objects do, you should try re-reading to get a better understanding!

Checkpoint 11.4.3.

You have attempted of activities on this page.