Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Random Access File

I am working with a data base program that includes class file. I want to define a function isNull() and a writeNullToFile in the Personal class. By doing this I have to modify the data bsae file to do the following.

define a function remove() in the data base, which locates the position of a record to be deleted and overwrites it with the null record. When this is done a destructor has to be invoked that copies the nonnull records to the new data file, deletes the old data file and renames the new data file with the name of the old data file.

Here is the .cpp

//database.cpp//

#include "personal.h"
#include "database.h"

template<class T>
Database<T>::Database() {
}

template<class T>
void Database<T>::add(T& d) {
database.open(fName,ios::in|ios::out|ios::binary);
database.seekp(0,ios::end);
d.writeToFile(database);
database.close();
}

template<class T>
void Database<T>::modify(const T& d) {
T tmp;
database.open(fName,ios::in|ios::out|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
cin >> tmp; // overloaded >>
database.seekp(-d.size(),ios::cur);
tmp.writeToFile(database);
database.close();
return;
}
}
database.close();
cout << "The record to be modified is not in the database\n";
}

template<class T>
bool Database<T>::find(const T& d) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
database.close();
return true;
}
}
database.close();
return false;
}

template<class T>
ostream& Database<T>::print(ostream& out) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (true) {
tmp.readFromFile(database);
if (database.eof())
break;
out << tmp << endl; // overloaded <<
}
database.close();
return out;
}

template<class T>
void Database<T>::run() {
cout << "File name: ";
cin >> fName;
cin.ignore(); // skip '\n';
database.open(fName,ios::in);
if (database.fail())
database.open(fName,ios::out);
database.close();
char option[5];
T rec;
cout << "1. Add 2. Find 3. Modify a record; 4. Exit\n";
cout << "Enter an option: ";
while (cin.getline(option,5)) {
database.clear();
if (*option == '1') {
cin >> rec; // overloaded >>
add(rec);
}
else if (*option == '2') {
rec.readKey();
cout << "The record is ";
if (find(rec) == false)
cout << "not ";
cout << "in the database\n";
}
else if (*option == '3') {
rec.readKey();
modify(rec);
}
else if (*option != '4')
cout << "Wrong option\n";
else return;
cout << *this; // overloaded <<
cout << "Enter an option: ";
}
}

int main() {
Database<Personal>().run();

return 0;
}
[3829 byte] By [redaccord02] at [2007-11-11 10:04:04]