#include #include "recform.h" #if defined(_WIN32) #define DEFAULTFILENAME "C:\\HOME01.DAT" #else #define DEFAULTFILENAME "/usr/local/csci/343/testdata/home01.dat" #endif int rangeSearch(ifstream &, char *, int, int, simpRecT &) ; void main(int argc, char *argv[]) { char key[KEYSIZE+80] ; simpRecT buff ; ifstream dbFile ; int maxRec ; #if defined(_WIN32) dbFile.open(DEFAULTFILENAME, ios::binary) ; #else dbFile.open(DEFAULTFILENAME) ; #endif if (!dbFile) { cout << "Unable to open " << DEFAULTFILENAME << endl ; return ; } dbFile.seekg(0, ios::end) ; maxRec = dbFile.tellg() / sizeof(simpRecT) - 1; if (maxRec < 0) { cout << DEFAULTFILENAME << " has no records!" << endl ; return ; } cout << "Enter search key: " ; while (cin.getline(key, sizeof(key)) && key[0]) { int pbeg = -1 ; int pend = -1 ; key[KEYSIZE] = '\0' ; cout << "Enter indices of starting and ending records" << endl ; while (pbeg < 0 || pbeg > maxRec) { cout << "Start (0 to " << maxRec << "): " ; cin >> pbeg ; } while (pend < pbeg || pend > maxRec) { cout << "End (" << pbeg << " to " << maxRec << "): " ; cin >> pend ; } cout << endl << "Starting search" << endl ; cout << " Key: " << key << endl ; cout << " Start: " << pbeg << endl ; cout << " End: " << pend << endl ; if (rangeSearch(dbFile, key, pbeg, pend, buff)) cout << "Record found with key " << buff.name << " and value " << buff.value << endl ; else cout << "No record found with key " << key << endl ; cin.ignore(1999, (int) '\n') ; // pretty silly cout << endl << "New key: " ; } dbFile.close() ; return ; }