1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include "person.h"
void CreateFile()
{
fstream cFile;
cFile.open("persons.bin", ios::in, ios::binary);
if (cFile.fail())
{
cFile.open("persons.bin", ios::out, ios::binary);
cout << "The file was created successfully" << endl << endl;
}
else
{
cout << "The database file already exists" << endl << endl;
}
}
void AddPerson()
{
fstream wFile;
Person p;
//Get info
cout << "Enter the first name: ";
cin >> p.fName;
cout << "Enter the last name: ";
cin >> p.lName;
cout << "Enter the age: ";
cin >> p.age;
cout << "Enter gender (M or F): ";
cin >> p.gend;
cout << "Enter address: ";
cin.ignore();
cin.getline(p.addy, 199);
wFile.open("persons.bin", ios::app, ios::binary);
wFile.seekp(0, ios::end);
wFile.write((char *) &p, (sizeof(Person)));
wFile.close();
cout << "Person was added successfully" << endl << endl;
}
void PrintDB() //INCOMPLETE
{
fstream pFile;
Person p;
int k = 0;
pFile.open("persons.bin", ios::in, ios::binary);
while (!pFile.eof())
{
pFile.seekg((sizeof(Person)*k), ios::beg);
pFile.read((char *) &p, sizeof(Person));
cout << k << ", " << p.fName << ", " << p.lName << ", " << p.gend << ", " << p.age << ", " << p.addy << endl;
k++;
}
pFile.close();
}
void DeletePerson() //INCOMPLETE
{
Person p;
int id;
fstream dFile;
strcpy(p.fName, "{EMPTY}");
strcpy(p.lName, "{EMPTY}");
strcpy(p.addy, "{EMPTY}");
p.age = 0;
p.gend = '?';
cout << "Enter ID of person to delete: ";
cin >> id;
dFile.open("persons.bin", ios::out, ios::binary);
dFile.seekp((sizeof(Person) * id), ios::beg);
dFile.write((char *) &p, sizeof(Person));
dFile.close();
cout << "The person was deleted successfully" << endl << endl;
}
void ModifyPerson() //INCOMPLETE
{
Person p;
int id;
fstream mFile;
cout << "Enter ID of person to modify: ";
cin >> id;
cout << "Enter the first name: ";
cin >> p.fName;
cout << "Enter the last name: ";
cin >> p.lName;
cout << "Enter the age: ";
cin >> p.age;
cout << "Enter gender (M or F): ";
cin >> p.gend;
cout << "Enter address: ";
cin.ignore();
cin.getline(p.addy, 199);
mFile.open("persons.bin", ios::out | ios::binary);
mFile.seekp((sizeof(Person) * id), ios::beg);
mFile.write((char *) &p, sizeof(Person));
mFile.close();
}
|