Contents

Instructions

This lab will give you experience with 1D arrays as well as file input/output.

Part 1: Basic File IO + Static Arrays

In this part, you will read in a file with 5 lines into a static array. You've used file IO quite a bit by this point, but if you need a refresher, see the File IO chapter.

In Sublime Text, create a new text file and save it as names.txt. In it, write down five names (these can be made up or names of friends or family), one name per line.

Now create a new C++ file called lab9-part1.cpp. Copy the following code into it:

Now write the code that corresponds to each of the comments in main.

Part 2: Reading a variable sized file

In this part, we will up the ante. Rather than only reading the first five lines of a file, we'll read the whole file. Moreover, we'll let the user specify the file! This, of course, means we'll need to use dynamic rather than static arrays, since we won't know the size of the array (i.e., the number of lines in the file) until the user actually chooses a file and we see how many lines it has.

Create a new file called lab9-part2.cpp and copy the following code to it. Then complete the program.

Part 3: Reading structured data from a file

Just reading lines from a file is a little boring. A more interesting use of files is to store information that we can read into a struct. That's what you'll do in this part.

Start by making a new text file called contacts.txt. In it, copy the names you entered in names.txt. For each name in the file, type an address for that person on the line below, and their phone number on the line below that. For example, here's a file with two entries (one for Barack Obama, and another for Doc Wylie):

Barack Obama
1600 Penn. Ave NW, Washington, DC
555-555-5555
Dr. Richard Wylie
376 Hale St., Beverly,  MA
444-444-4444

Now create a C++ file called lab9-part3.cpp and copy the code below to it. Complete the program.

Part 4: Command line arguments

So far, we've executed our compile programs by doing something like:

$ ./lab9-part3

However, we've seen lots of executables where you can pass arguments. For example, if you want to change into your Desktop from your home directory, you have to do:

$ cd Desktop

In this case, "Desktop" is an argument we're passing to the cd program. Lots of other programs work like this, such as g++, to which we usually pass three arguments: the name of the C++ file to compile, a "-o" flag, and the name of the file we'd like to save the executable as.

So, how do we do that? It's pretty simple, actually. Change the signature of main from this:

to this:

The first parameter, argc, is an integer that hold the number of command line arguments passed in, plus one (the program name itself counts as an argument). The second parameter, argv is an array of arrays of characters (a 2D character array). This is also called a 1D array of C strings (a C string is nothing more than an 1D character array). To get the first argument (which is always the name of the program executable), do: argv[0]. To convert it to a C++ string, do: string(argv[0]). Here's how we can save the name of the executable as a C++ string and then print it:

The first non-executable-name argument (the first "real" argument) is stored at index 1 in the argv array, provided one was passed in. For example, if we did:

$ ./my-executable Hello

and we wanted our program to be able to read the argument "Hello", we could access it like this: argv[1].

You should always check that you have as many arguments as you expect. If you want the user to provide three arguments (like g++ does), then you should ensure that argc == 4 (that's three arguments, plus the name of the executable). If the expected arguments are not passed in, display an error message. Here's a program that expects one "real" argument, a name.

Suppose we saved that program in a file named display-name.cpp and compiled it to an executable named display-name. If we run it like we usually run program, we'll get an error:

$ ./display-name
Error: too few arguments.
Usage: ./display-name <name>

If we give it a name, like "Beth", here's what happens:

$ ./display-name Beth
Hello, Beth! Nice to meet you!

What if you want a longer name? Just surround the name with double quotes. That tells the commandline environment to pass in everything between the quotes as a single argument. Here's how we can pass in the name "Beth Rosemary Jones":

$ ./display-name "Beth Rosemary Jones"
Hello, Beth Rosemary Jones! Nice to meet you!

Now, for this part of the lab, you should copy your code from Part 3 to a file named lab9-part4.cpp and alter it so that the program uses a command line argument to get the file name instead of asking the user as part of the program. You should be able to run your program like this:

$ ./lab9-part4 contacts.txt

Your program should not prompt the user for the filename later.

Submission

Submit your .cpp files to here. You can view the rubric here.

(Back to top)