Sams Teach Yourself C in 21 Days

(singke) #1
In Review 625

that there is no limit to the number of people who can be entered into the program. This
is because a disk file is used for data storage.
When you start the program, you enter the name of the data file on the command line.
main()starts on line 38 with the argcandargvarguments required to get the command-
line parameters. You learned about this on Day 21, “Advanced Compiler Use.” Line 43
checks the value of argcto see how many parameters were entered on the command
line. If argcis less than 2 , only one parameter was entered (the command to run the pro-
gram), which means that the user didn’t specify a data filename. In this case, the pro-
gram calls display_usage()withargv[0]as an argument. argv[0], the first parameter
entered on the command line, is the name of the program.
Thedisplay_usage()function is on lines 188 through 193. Whenever you write a pro-
gram that takes command-line arguments, it’s a good idea to include a function similar to
display_usage()that tells the user how to use the program. Why doesn’t the function
just hard-code the name of the program (week3) instead of using the command-line argu-
ment? The answer is simple. When you obtain the program name from the command
line, you don’t have to worry if the user renames the program; the usage description is
always accurate.
Most of the new concepts in this program come from Day 16, “Using Disk Files.” Line
40 declares a file pointer fpthat is used throughout the program to access the data file.
Line 50 tries to open this file with a mode of “a+”(remember,argv[1]is the second
item listed on the command line—the data filename). The “a+”mode is used because
you want to be able to add to the file as well as read any records that already exist. If the
file open operation fails, lines 52 and 53 display an error message before line 54 exits the
program. Notice that the error message contains descriptive information. Also notice that
__LINE__, covered on Day 21, indicates the line number where the error occurred.
If the file is opened successfully, a menu is displayed. When the user chooses to exit the
program, line 74 closes the file with fclose()before the program returns control to the
operating system. Other menu options enable the user to enter a record, display all
records, or search for a particular person.
In the get_data()function, there are a few significant changes. Line 101 contains the
function header. The function now accepts three pointers. The first pointer is the most
important: It is the handle for the file to be written to. Lines 105 through 126 contain a
whileloop that continues to get data until the user wants to quit. Lines 107 to 116
prompt for the data in the same format that the program in Week 2 in Review did. Line
118 calls fseek()to set the pointer in the disk file to the end to write the new informa-
tion. Notice that this program doesn’t do anything if the seek fails. A complete program

34 448201x-W3R 8/13/02 11:22 AM Page 625

Free download pdf