Concepts / Reading Data from a File with ifstream

Reading Data from a File with ifstream

Reading and writing point in opposite directions. ifstream moves data into the program, and ofstream moves data out of the program into a file.

  • Programming

Why Programs Need File Streams

A program often needs to get information out of a file, put information into a file, or do both. C++ handles this work through file streams. File streams are used for reading from and writing to files, so they are the tools a program uses to move data between itself and a file on disk.

File streams do not all play the same role. Three stream types are covered here, and each one suits a different kind of job. The question this section answers is simple: when you open a file in a program, which stream type should you pick? Before you can choose, you need a clear picture of what each type is for.

Three Stream Types at a Glance

There are different types of file streams for reading and writing. The three named here are ifstream, ofstream, and fstream. Each one is a stream object that a program can use with a file. The name of each type hints at its job: ifstream is for input, ofstream is for output, and fstream is for both.

Definition

ifstream is the stream type for reading from a file, supporting input operations. ofstream is the stream type for writing to a file, supporting output operations. fstream is the stream type for both reading from and writing to a file.

ifstreamReading from a file (input)ofstreamWriting to a file (output)fstreamReading and writing (both)
How do ifstream, ofstream, and fstream differ in whether they read, write, or both?

ifstream: Data Coming Into the Program

Think of ifstream as the stream you choose when the program is the receiver of the file's contents. Its name comes from input, which is the direction in which data flows into the program. If your task is to pull data from a file so the program can work with it, ifstream matches that task.

File on diskStored contentsifstreamInput stream objectProgram variableData the program works withdata flows out of filedata flows into program
In which direction does data move when reading with ifstream, and how does it get from the file on disk into a program variable?

Read the arrows in the diagram from left to right. The file is the source, the stream is the channel, and the program is the destination. Reading means data travels from the file into the program, which is exactly what the input direction of ifstream describes.

ofstream: Data Going Out to a File

ofstream is the mirror image of ifstream. Where ifstream carries data into the program, ofstream carries data out of the program and into a file. When the task is to record results, save output, or produce a file, ofstream is the stream type that fits.

Key point

Reading and writing point in opposite directions. ifstream moves data into the program, and ofstream moves data out of the program into a file.

fstream: Reading and Writing Together

fstream covers the jobs of both ifstream and ofstream in one type. When a program must read from a file and also write to it, fstream is the stream type the source names for that combined work.

Choosing a Stream by Direction

Choosing a stream type comes down to one question: what does the program need to do with the file? The source gives a direct mapping from job to type. Read the task, identify the direction of the data, and pick the matching stream.

A decision path from the task to the stream type.

What do you think happens?

A program needs to pull a list of scores out of a file so it can compute an average. Which stream type fits this task?

  • ifstream

  • ofstream

  • fstream

Reveal answer
Answer:

ifstream

The task pulls data from the file into the program so the program can work with it. The source identifies ifstream as the stream type for reading from a file.

Mistakes When Picking a Stream Type

  • Common mistake

    Choosing ofstream to read a file because the file is the thing being opened

    What it looks like

    A program that loads saved settings from a file uses ofstream.

    Why it fails

    The direction is what matters, not the fact that a file is opened. Loading settings moves data into the program, which is the input direction.

    Do this instead

    Use ifstream when the program receives the file's contents.

  • Common mistake

    Assuming fstream is needed for every file task

    What it looks like

    A task that only records results is given an fstream.

    Why it fails

    The source says fstream is for both reading and writing. A task that only writes matches ofstream.

    Do this instead

    Pick fstream only when the program must both read from and write to the file.

  • Common mistake

    Ignoring the direction of data and choosing by which type sounds familiar

Practice Matching Tasks to Streams

EASY

For each task, name the stream type you would choose: (a) a program saves a game's final score to a file; (b) a program reads a list of names from a file and also appends new names to that same file; (c) a program reads a configuration file at startup.

Hints
  • Ask first whether data moves into the program, out of the program, or both.
  • Task (a) moves data out of the program into a file.
  • Task (b) needs both directions, so look for the type the source names for combined work.

Key Takeaways

  • C++ file streams move data between a program and a file on disk, for reading, writing, or both.
  • ifstream is the stream type for reading from a file and carries data into the program.
  • ofstream is the stream type for writing to a file and carries data out of the program.
  • fstream is the stream type for both reading from and writing to a file.
  • Choose a stream type by asking what the program needs to do with the file, then match that direction to ifstream, ofstream, or fstream.

Related concepts

More concepts starting with R