Topical Information

This lab should provide you with practice in file handling and string manipulation. Classes are pretty much useless here. *grin*

Program Information

Write a program that allows the user to search through a list of names stored in a file for a particular one. (Make sure to allow the user to tell you the name of their file and to check for file open errors.) The names in the file will be stored one per line (spaces are allowed in the names). You won't know the length of the list of names ahead of time and it could be a ridiculous number of names — hundreds of thousands or even millions!

Although this might seem like the perfect place to use strcmp, most people think of searches as case insensitive — and strcmp is case sensitive. You'll have to write your own version of strcmp which is case insensitive to use in the program. (Maybe strcmp_ncase?)

You can choose the actual names to use for your data. Make sure to do enough sets of data to well test your program! (Empty data set, find first item, find last item, find a few in the middle, etc.)

Remember that your program cannot know how many data names are in the file ahead of time! In this context, this means that you cannot store all the names in your program's memory — not even using dynamic memory or a vector.

Don't forget to read the file's name from the user and protect your program against any errors that may occur during the opening of the file.

Use functions to break up the program into more manageable pieces.


As an example, you might have the data file contain:

Jason James
Mary Jones
Tammy Henry
Bob Smith

and the program interaction might look something like (the parts in this color are typed by the user):

$ namesearch.out

                 Welcome to the Name Searching Program!!!

Please enter the name of your names file:  bob.dat

I'm sorry, I could not open 'bob.dat'.  Please enter another name:
names

File 'names' opened successfully!

What name would you like to find in this file?  tammy henry

'Tammy Henry' is the 3rd name in the file!

Thank you for using the NSP!!

Endeavor to have an amazing day!

$

Don't forget about the occasion when the name they want isn't in the file at all!

If you want it to look nice (with the rd on 3rd instead of just 'position 3'), please see the option below.

Thought Provoking Questions

  1. How do you handle not knowing how much data is in the file?

  2. What kind of loop do you use to process the file?

  3. What do you do if the person isn't found in the file?

  4. Can there be more than one name on a single file line? (Would your program handle it? Don't change it to make it — just answer the question.)

This assignment is (Level 1.5).

Options