Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 EPSII 59:006 Spring 2004. 2 Random Access Files—A Design Example Problem statement:  Consider a very large file—e.g. millions of records  Impractical.

Similar presentations


Presentation on theme: "1 EPSII 59:006 Spring 2004. 2 Random Access Files—A Design Example Problem statement:  Consider a very large file—e.g. millions of records  Impractical."— Presentation transcript:

1 1 EPSII 59:006 Spring 2004

2 2 Random Access Files—A Design Example Problem statement:  Consider a very large file—e.g. millions of records  Impractical to maintain in sorted fashion sorting would be very expensive—need to swap lots of very large records in the file too time-consuming to re-sort the file each time a new record is added or a record is changed.  But we want to be able to locate records efficiently, using some “Key Value”.  This requires an efficient search technique, such as binary search, but this only works on a sorted list.  How can we solve this dilemma?????

3 3 The Solution: General Approach:  maintain a separate, sorted array (or file) of key values. Call this the Key Array.  For each key value in the Key Array, keep track of the location in the file of the corresponding record  To locate a record with a specific key value, do a binary search of the Key array to locate the matching element.  Use the location information extracted from the key array to directly access the desired record from the file

4 4 General Approach: record 0 record 1 record 2 … record 500 … record 10,000 …… key = “Doe”key = “Zyss”key =“Jones”key=“Aaron”key = “Simpson” 0499500999 10001499 “Aaron”250,000...... “Doe” 0............ “Jones”1000...... “Simpson”...... “Zyss” 5,000,000 500........................ 250,0005,000,000 0 10,000 The File: The Key Array: 500 bytes

5 5 A “Real” Example Consider the credit.dat file used in the examples 11.11- 11.16 (previous lecture) The record format looks like: struct clientData { int acctNum; char lastName[15]; char firstName[10]; double balance; }; Let’s create a key array that will allow us to locate records in credit.dat by the client’s last name.

6 6 Algorithm for Creating the Key Array Open the credit.dat file for reading (“r”) read the credit.dat file sequentially, placing each key value (last name) and corresponding record location(byte offset) into the next element of the key array Sort the key array according to name Now we are ready to perform binary searches on the key array to locate records by the client’s last name

7 7 The key array: struct keyElement { char lastName[15];//this is the key value intrecordLocation; //bye offset from //beginning of file } struct keyElement keyArray[101]; //Note that keyArray is an array of structures //Note: keyArray[0] won’t be used, since records //of client.dat are numbered 1-100.

8 8 Refinement of algortihm Second step: for each record i (1 <= i <=100) in the credit.dat file do the following: keyArray[i].lastName = lastName field of record i keyArray[i].recordLocation = (i-1)*sizeof(struct clientData) Third step: Just do a standard bubble sort of KeyArray based upon the values of the lastName fields.

9 9 Creating and sorting the keyArray #include #define NUM_RECS 100 struct clientData { int acctNum; char lastName[15]; char firstName[10]; double balance; }; struct keyElement { char lastName[15]; int recordLocation; }; //function templates void sortKeys(struct keyElement [], int); int searchKeys(struct keyElement [], char *, int); if ((fPtr=fopen(argv[1], "rb"))==NULL) printf("Error opening file %s\n", argv[1]); else { for (i=1; i<=NUM_RECS; i++) { fread(&clientRec, sizeof(struct clientData),1, fPtr); strcpy(keyArray[i].lastName, clientRec.lastName); keyArray[i].recordLocation = (i-1)*sizeof(struct clientData); } // Bubble sort the key Array sortKeys(keyArray, NUM_RECS); //Just does a bubble sort //Print out the sorted keyArray(non-null entries only) printf("keyArray Index\t lastName\tLocation\n"); for(i=1; i<=NUM_RECS; i++) if (strcmp(keyArray[i].lastName, "")!=0) printf("%14d\t%15s\t%8d\n", i, keyArray[i].lastName, keyArray[i].recordLocation); int main(int argc, char *argv[]) { FILE *fPtr; struct clientData clientRec; struct keyElement keyArray[NUM_RECS+1]; int i, pos; char name[15];

10 10 Locating records using the keyArray //Now prompt user to enter names to search for in the file. For each valid name entered, //print the entire record. //Enter "quit" to end printf("Enter the last name of a client whose record you wish to display\n"); //fgets(name, 15, stdin); gets(name); while(strcmp(name, "quit")!= 0) { pos = searchKeys(keyArray, name, NUM_RECS); //performs binary search on keyArray if (pos != 0) { fseek(fPtr, keyArray[pos].recordLocation, SEEK_SET); fread(&clientRec, sizeof(struct clientData), 1, fPtr); printf("Located Record for: %s\n", clientRec.lastName); printf("\tLast Name = %s, \tFirst Name =%s\n", clientRec.lastName, clientRec.firstName); printf("\tAccount Number = %d\n", clientRec.acctNum); printf("\tBalance = %.2lf\n", clientRec.balance); } else printf("Record Not Found\n"); printf("Enter the last name of a client whose record you wish to display\n"); // fgets(name, 15, stdin); gets(name); } printf("Goodbye\n"); } } //end of main()

11 11 Finally, the function definitions void sortKeys(struct keyElement keyList[], int size) { /* bubble sort */ int i, pass; struct keyElement hold; /* loop to control number of passes */ for ( pass = 1; pass < size; pass++ ) { for ( i = 1; i < size; i++ ) { if(strcmp(keyList[i].lastName,keyList[i+1].lastName)>0) { hold = keyList[i]; keyList[i] = keyList[i+1]; keyList[i+1] = hold; } } //End of sortKeys() int searchKeys(struct keyElement keyList[], char *keyValue, int size) { //binary search int lower = 1, upper = size; int middle; int not_found = 1; while ((upper >= lower) && not_found) { middle = (lower + upper)/2; if (strcmp(keyList[middle].lastName, keyValue) <0) lower = middle+1; else if (strcmp(keyList[middle].lastName, keyValue) > 0) upper = middle -1; else not_found = 0; } if(not_found) return 0; else return middle; } //end of searchKeys()

12 12 Example Output Suppose that the contents of credit.dat, as displayed by the program in fig.11.15(corrected) are: Acct Last Name First Name Balance 1 Doe John 5.00 45 Smith Mary 200.00 100 Jones Fred 20.00 Now Run our keyLocate Application: %KeyLocate credit.dat keyArray Index lastName Location 98 Doe 0 99 Jones 3960 100 Smith 1760 Enter the last name of a client whose record you wish to display Smith Located Record for: Smith Last Name = Smith, First Name =Mary Account Number = 45 Balance = 200.00 Enter the last name of a client whose record you wish to display


Download ppt "1 EPSII 59:006 Spring 2004. 2 Random Access Files—A Design Example Problem statement:  Consider a very large file—e.g. millions of records  Impractical."

Similar presentations


Ads by Google