//option 8 void WriteTextArchiveAndDeleteBinary ( fstream& ioFile, bool& successful ) { ioFile.clear(); string outputFile; ReadThisLine("Enter name of output textfile for archiving " "club member data: ",outputFile); ofstream outFile(outputFile); if(!outFile) { cout << "\nError opening out textfile.\nReturning to menu." << endl; Pause(); successful = false; } else if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { //start at beginning of file ioFile.seekg(0); while(!ioFile.eof()) { //read the binary file ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //store the info from binary file back into a txt file outFile << member.name << endl; outFile << member.age << " $" << member.balance << endl; } } successful = true; //clear the txtfile as well as the binary file outFile.clear(); outFile.close(); ioFile.clear(); ioFile.close(); //delete the binary file remove("working.bin"); } }
//option 5 void DisplayOneOrMoreMembers ( fstream& ioFile, bool& successful ) { ioFile.clear(); if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { string searchedName; ReadThisLine("Enter full or partial name of member(s) whose data " "you wish to display:",searchedName); //start at the beginning of file ioFile.seekg(0); while(!ioFile.eof()) { ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //if found the searchedName(full or partial) if(((string)member.name).find(searchedName) != string::npos) { //print it cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; } } } successful = true; } }
//option 6 void AddNewMember ( fstream& ioFile, bool& successful ) { if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { ioFile.clear(); string newMemberName; int newMemberAge; double newMemberBalance; //ask for info for the new club member ReadThisLine("Enter name of new member: ",newMemberName); ReadInt("Enter age of new member: ", newMemberAge); ReadDouble("Enter opening balance of new member: ",newMemberBalance); //clear the array memset(member.name,'\0',31); for(unsigned int i = 0 ; i < newMemberName.length(); i++) member.name[i] = newMemberName[i]; //store the info into the struc member.age = newMemberAge; member.balance = newMemberBalance; ioFile.seekg(0, ios::end); //write the struc into the binary file ioFile.write((char *)&member, sizeof(ClubMember)); ioFile.seekg(0); successful = true; } }
//option 7 void UpdateMemberData ( fstream& ioFile, bool& successful ) { //store the position of the stream(later to be used for seekg/p) streamoff position; ioFile.clear(); int count = 0; if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { string identity; ReadThisLine("Enter enough of the member's name to " "identify him/her: ", identity); //start at beginning ioFile.seekg(0); while(!ioFile.eof()) { //make a temporary position to hold the beginning of the struc streamoff tempPos = ioFile.tellg(); //read the binary file ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { if(((string)member.name).find(identity) != string::npos) { //put the beginning of the struc into the position position = tempPos; //print club member cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; count++; } } } if(count != 1) { cout << "\n=====>Either more than one member, or no members, " "matched your input.\n=====>You need to identify a " "unique club member to update.\n=====>Try again with " "more specific input.\n" << endl; Pause(); successful = false; } if(count == 1) { cout << "\nUpdating data for the above club member." << endl; string choice; do { cout << "Enter n/a/b to update name/age/balance, or " "q to quit updating this member: "; getline(cin,choice); if(choice == "q") { cout << "\nOK, no more updates for this club member " "at this time." << endl; Pause(); successful = true; } if(choice == "n") { string revisedName; ReadThisLine("Enter revised name: ", revisedName); //clear the array memset(member.name,'\0',31); for(unsigned int i = 0 ; i < revisedName.length(); i++) //store the new name into the struc member.name[i] = revisedName[i]; } if(choice == "a") { int newAge; ReadInt("Enter revised age: ", newAge); //store new age in struc member.age = newAge; } if(choice == "b") { double newBalance; ReadDouble("Enter revised balance: ", newBalance); //store new balance in struc member.balance = newBalance; } } while(choice != "q"); } ioFile.clear(); //go to the position ioFile.seekg(position); ioFile.seekp(position); //finally write the info back into the binary file ioFile.write((char *)&member, sizeof(ClubMember)); successful = true; } }