Contents

Introduction

Up to this point, we have only made programs that interact with data provided by a user. We ask the user for a number, then read the number in. Or we ask the user for a string, and then we read it in. Many times, it's useful to interact with a file, like a database or saved data. For instance, every time you open a Word document or PowerPoint slide show, Microsoft Office is reading from and writing to that file.

In this class, we will specifically consider plain text files, by which I mean that they are not in a special format (like a .docx or .pdf).

(Back to top)

Reading files

To read a program, we need to include the fstream library (read: file stream). We then create an instance of type ifstream (read: input file stream). We then have to open the file, read from the file, and then close the file. Closing the file is important! So always make sure you do it as soon as your program is done with a file. Here is a program that asks a user to enter a file name, then prints out the first word in that file on its own line:

readFile.cpp

A few things to notice. First, to open and close our file, we must invoke functions on our ifstream instance. This is similar to cin.ignore(). Second, when we open, we need to pass not a regular string, but a C-styled string (as in the C that came before C++). We stored fileName as a string, so to convert it, we just need to invoke the function c_str() on our string instance, in this case, fileName.c_str(). Finally, reading from a file looks a lot like reading from cin: fileIn >> currentWord.

Here's a text file we can use to test this. Open up Sublime or another text editor and copy/paste this text. Then save the file in the same directory as the C++ program from above, giving it the name test.txt.

test.txt
This is an example of a text file with nothing useful in it.

Here's the output:

$ ./readFile
Please enter the name of a file to print: test.txt
This

Now, what if we want to read out a whole file, we need to keep reading from a file until nothing more can be read. We can perform this check by putting the read line inside a while condition:

If we want to read every word in the file and print it out to the terminal on its own line, our program will look like this:

readWholeFile.cpp

And here's the output of this version of the code.

$ ./readWholeFile
Please enter the name of a file to print: test.txt
This
is
an
example
of
a
text
file
with
nothing
useful
in
it.
(Back to top)

Writing files

Writing files is easier than reading them! We need to include the same library we included to read: fstream. However, we need to make an instance of the type ofstream this time (read: output file stream). Like with reading, we have to open, write, and then close the file. Unsurprisingly, ofstream behaves very similar to cout. Here's an example of a program that writes the message "Hello from C++!" to a file specified by the user:

writeFile.cpp

Here's what it looks like when we run it:

$ ./writeFile 
Please enter the name of a file to write to: hello.txt

Now we should see that a file hello.txt has been created. Go ahead and open it with Sublime or another text editor. You should see the following text:

Hello from C++!
(Back to top)

Advanced File IO

There are many advanced features of file IO, such as appending to an existing file. I won't cover these, but I encourage you to take a look at this helpful overview of C++ file IO for more information.

(Back to top)