Lab 10: File IO
Contents
Instructions
This lab has two parts about parsing (reading and interpreting) input files. In the first part, you'll calculate the percentage of area a county takes up in its state and in the second part, you'll scan through a file to find a contact's information.
Part 1: County area
For this part, you will create a C++ program to parse a file with two columns: a place name (e.g., a county within a state) followed by an area in square miles. The program should add a third column to the data in a new file, which is, for each line of data, the percentage of that county's area relative to the total area across all counties. So, for example, for an input file that looks like this:
Barnstable 396 Berkshire 931 Bristol 556 Dukes 104 Essex 498 Fraklin 702 Hampden 618 Hampshire 529 Middlesex 824 Nantucket 48 Norfolk 400 Plymouth 661 Suffolk 58 Worcester 1513
your program should produce an output file that looks like this:
Barnstable 396 0.05 Berkshire 931 0.12 Bristol 556 0.07 Dukes 104 0.01 Essex 498 0.06 Fraklin 702 0.09 Hampden 618 0.08 Hampshire 529 0.07 Middlesex 824 0.11 Nantucket 48 0.01 Norfolk 400 0.05 Plymouth 661 0.08 Suffolk 58 0.01 Worcester 1513 0.19
Write your program in a file called lab10-part1.cpp.
Your program should read the name of the input file, the name of the output
file, then carry out the computation. It should ensure that the input file
exists before trying to read data from it, and if not, ask the user for another
filename until one is found that exists. This is fairly simple to test:
after opening the file (e.g., inFile.open(filename.c_str());
),
test if the file is good: inFile.good();
. The good()
method will return true if the file exists and can be read.
You may assume that the county name will always be a single word (no spaces).
HINTS: You may want to consider reading the file twice, or storing the data you read in the first time in a way that lets you iterate over it again. Also, when you go to run your program, provide an output filename that is different from your input file name, or else you'll overwrite your input file.
Your program should be able to handle any input in the format described earlier. For instance, it should handle processing a file with the following New Jersey county areas:
Atlantic 569 Bergen 234 Burlington 819 Camden 221 Cape-May 267 Cumberland 500 Essex 130 Gloucester 329 Hudson 47 Hunterdon 423 Mercer 228 Middlesex 312 Monmouth 476 Morris 468 Ocean 642 Passaic 192 Salem 365 Somerset 307 Sussex 527 Union 103 Warren 362
Challenge: Formatting can be tricky. See if you can figure out how to get the spacing in the output file to look like the example above, and display the percentage to two decimal places.
Part 2: Contact
Write a C++ program that will prompt a user for a file of contacts and for the last name of a contact to search for. If the file does not exist, the program should report the error "File not found" and ask the user for another filename until one is given that is found. The program should then search the file for that contact's last name. If found, the program should print out the contact's information (phone number, email, etc.) to the console, and if not found, it should print "Contact not found." to the console. The contact file will consist of the following four lines of text per contact:
last name first name phone # email
Each line may consist of spaces (for instance, a last name could be "Van Helsing"), so be sure that your method for reading each piece of information can handle that. Here's an example input file:
De Niro Robert (555) 444-3333 bdeniro@hollywood.com Pachino Al (555) 444-3334 al@mafia.com Hanks Tom (777) 555-4444 th@big.com Streep Meryl (333) 555-3232 streep@hotmail.com Hepburn Audrie (492) 492-4924 ahep@movies.com
Try creating your own!
Challenge: Adapt the program so that it asks for the file once, but then keeps asking the user for names until the user indicates they want to stop (using a sentinel value).
Submission
Submit your .cpp files from parts 1 and 2 to here.
(Back to top)