#include "Datafile.h" #include "Bucketfile.h" #include "Directoryfile.h" #include "Hash.h" #include "Directory.h" #include "Bucket.h" #include #include #include #include // for setw, setiosflags, etc... using namespace std; const int number_length = 8; const int lastname_length = 10; const int firstname_length = 10; const int credits_length = 3; const int CGPA_length = 4; int i=0; int key=-1; int depth=-1; // set the record size, which is the sum of the lengths // of the student number field, the name field, and // extra end-of-line characters const int recsize = number_length + lastname_length + firstname_length + credits_length + CGPA_length + 2; // read a student from a istream void readDatafile(int, istream&, Datafile&); // write a student in the ostream void writeDatafile(ostream&, Datafile); // main method int main(int argc, char* argv[]) { //check if the use of command line arguments are correct if ((argc == 5)&&(strcmp(argv[2], "EHdirectory.txt")==0)&&(strcmp(argv[3], "EHbuckets.txt")==0)&&(strcmp(argv[4], "log.txt")==0)) { Datafile datafile; // a student fstream input; // input stream fstream output_directory; // output stream fstream output_bucket; // output stream fstream output_log; // output stream char inputname[40]; // input filename char directory_fileName[40]; // output filename char bucket_fileName[40]; // output filename char log_file[40]; // output log.txt strcpy(inputname,argv[1]); strcpy(directory_fileName, argv[2]); strcpy(bucket_fileName,argv[3]); strcpy(log_file,argv[4]); // open the input file input.open(inputname, ios::in); // Check to make sure that we could open the files properly! if (input.fail()) { cerr << "Could not open file "; cerr << inputname; cerr << ".\n"; return 1; } int result; Directory Dir(5); result = Dir.Create(directory_fileName,bucket_fileName,log_file); if(result ==0) { cout<<"Please delete EHdirectory.txt and EHbucket.txt and log.txt"<> studentNum; if (studentNum==-1) { return 0; break; } // open the input file input.open(inputname, ios::in); // Check to make sure that we could open the files properly! if (input.fail()) { cerr << "Could not open file "; cerr << inputname; cerr << ".\n"; return 1; } // open the files Directoryfile directoryfile; input_directory.open(directory_fileName, ios::in); // Check to make sure that we could open the files properly! if (input_directory.fail()) { cerr << "Could not open file "; cerr << directory_fileName; cerr << ".\n"; return 1; } directoryfile.readDirectoryfile(0,input_directory); depth=directoryfile.getKey(); address=hash(studentNum, depth); directoryfile.readDirectoryfile(address+1,input_directory); recAddress=directoryfile.getKey(); Bucketfile theBucket; input_bucket.open(bucket_fileName, ios::in); // Check to make sure that we could open the files properly! if (input_bucket.fail()) { cerr << "Could not open file "; cerr << bucket_fileName; cerr << ".\n"; return 1; } theBucket.readBucketfile(recAddress,input_bucket); if (studentNum==0) { cout<<"No such record in Index,please check it out"<"<< " "<<" " <<" \n"; cout << "\n Usage For search: " << argv[0] << " "<< " \n"; return 1; } } //read a student from a istream void readDatafile(int rrn, istream& input, Datafile& s) { string number_str; string lastname_str; string firstname_str; string credits_str; string CGPA_str; long int number; int credits; int credits_term=0; float CGPA; string line; // seek to the appropriate student s.InputPositioningByRRN(rrn, input); // read in a line from fine getline(input,line,'\n'); // get the number and last name and first name and credits and CGPA strings number_str = line.substr(0,number_length); lastname_str = line.substr(number_length,number_length); firstname_str = line.substr(number_length+lastname_length,lastname_length); credits_str = line.substr(number_length+lastname_length+firstname_length,credits_length); CGPA_str = line.substr(number_length+lastname_length+firstname_length+credits_length,CGPA_length); // convert the number string into a long int. number = atol((number_str.c_str())); // convert the credits string into a int. credits = atoi((credits_str.c_str())); // convert the CGPA string into a float CGPA = (float)atof((CGPA_str.c_str())); // set number and lastname and firstname and credits and CGPA for student s s.setNumber(number); s.setLastName(lastname_str); s.setFirstName(firstname_str); s.setCredits(credits); s.setCredits_term(credits_term); s.setCGPA(CGPA); }