Previous: 13.2 Tell and Seek
Up: 13 Files and the Operating System
Next: 13.4 Operating System Interface
Previous Page: 13.2 Tell and Seek
Next Page: 13.4 Operating System Interface

13.3 A Small Data Base Example

A data base is a collection of a large set of data. We have seen several examples of data bases in previous chapters, such as our payroll data and the list of address labels discussed in Chapter . In our programs working with these data bases we have simply read data from files, possibly performed some calculations, and printed reports. However, to be a useful data base program, it should also perform other management and maintenance operations on the data. Such operations include editing the information stored in the data base to incorporate changes, saving the current information in the data base, loading an existing data base, searching the data base for an item, printing a report based on the data base, and so forth. Programs that manage data bases can become quite elaborate, and such a program to manage a large and complex data base is called a Data Base Management System (DBMS).

In this section we will implement a rather small data base system that maintains our data base for address labels. We will assume that there are separate lists of labels for different groups of people; therefore, it should be possible to save one list in a file named by the user as well as to load a list from any of these files. The data in a list of labels is mostly fixed; however, it should be possible to make additions and/or changes. It should also be possible to sort and search a list of labels.

In our skeleton data base system, we will not implement sorting and searching operations (we have already implemented a sort function for labels in Section ), instead, our purpose here is to illustrate some of the other operations to see the overall structure of a DBMS. We will implement operations to add new labels, print a list of labels, as well as loading and saving lists in files. The program driver will be menu driven. The user selects one of the items in the menu, and the program carries out an appropriate task. The data structures we will use include the label structure and a type, label, defined in the file lbldb.h shown in Figure 13.5. The program driver is shown in Figure 13.6.

The list of labels is stored in the array, lbllist[], and n stores the actual number of labels, initially zero. A new list is read by the function load() which returns the number of labels loaded. A list can be edited by edit() which updates the value of n. Both edit() and load() must not exceed the maximum size of the array. The functions print() and save() write n labels from the current list.

Figure 13.7 shows a partial implementation of edit(), allowing only the addition of new labels. It does not implement operations for deletion or change of a label.

The edit() function presents a sub-menu and calls the appropriate function to perform the task selected by the user. We have included program ``stubs'' for the functions del_label() and change_label() which are not yet implemented. The add_label() function calls on readlbl() to read one label. If a label is read by readlbl() it returns TRUE; otherwise, it returns FALSE. The loop that reads labels terminates when either the maximum limit is reached or readlbl() returns FALSE. Each time readlbl() is called, n is updated, and the updated value of n is returned by add_label(). In turn, edit() returns this value of n to the main driver.

The function readlbl() first reads the last name, as shown in Figure 13.8. If the user enters an empty string, no new label is read and the function returns FALSE; otherwise, the remaining informations for a label is read and the function returns TRUE.

The print() function calls on printlabel() to print a single label data to the standard output. The functions are shown in Figure 13.9.

Finally, we are ready to write functions load() and save(). We will use fread() and fwrite() to read or write a number of structure items directly from or to a binary file. This method of storing the data base is much more efficient that reading ASCII data, field by field for each label. The code is shown in Figure 13.10. The function load() opens an input file, and uses fread() to read the maximum possible ( lim) items of the size of a label from the input file. The buffer pointer passed to fread() is the pointer to the array of labels, lbllist. Finally, load() closes the input file and returns n, the number of items read. Similarly, save() opens the output file, and saves n items of label size from the buffer to the output file. It then closes the output file and returns n. If it is unable to open the specified output file, it returns 0. A sample session is shown below.

The session starts with the menu menu. We select the menu item Load to load a previously saved list of labels in the file, lbl.db. After this file is loaded, we select Print to print the labels. Next, we select Edit and Add to add one new label. Then we select Save to save the revised list to the file lbl.db. Finally, we select Quit to exit the program.



Previous: 13.2 Tell and Seek
Up: 13 Files and the Operating System
Next: 13.4 Operating System Interface
Previous Page: 13.2 Tell and Seek
Next Page: 13.4 Operating System Interface

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:17:28 HST 1994