I do not know where to start with this requirement. I know I have to int isNull() and write it to a file, but not really know how to const it.
Just need an example on how to start this, thanks
Need to define function isNull() in personal class and define function writeNullToFile to overwrite a record to be deleted by a null record. The null record is defined as having a nonnumeric character (a tombstone) in the first position of the SSN member.
Code:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
#ifndef PERSONAL
#define PERSONAL
class Personal
{
public:
Personal();
Personal(char*,char*,char*,int,long);
void writeToFile(fstream&) const;
void readFromFile(fstream&);
void readKey();
int size() const
{
return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
}
bool operator==(const Personal& pr) const
{
return strncmp(pr.SSN,SSN,9) == 0;
}
protected:
const int nameLen, cityLen;
char SSN[10], *name, *city;
int year;
long salary;
ostream& writeLegibly(ostream&);
friend ostream& operator<<(ostream& out, Personal& pr)
{
return pr.writeLegibly(out);
}
istream& readFromConsole(istream&);
friend istream& operator>>(istream& in, Personal& pr)
{
return pr.readFromConsole(in);
}
};
#endif
#include "personal.h"
Personal::Personal() : nameLen(10), cityLen(10)
{
name = new char[nameLen+1];
city = new char[cityLen+1];
}
Personal::Personal(char ........n, char *n, char *c, int y, long s) :
nameLen(10), cityLen(10)
{
name = new char[nameLen+1];
city = new char[cityLen+1];
strcpy(SSN,ssn);
strcpy(name,n);
strcpy(city,c);
year = y;
salary = s;
}
void Personal::writeToFile(fstream& out) const
{
out.write(SSN,9);
out.write(name,nameLen);
out.write(city,cityLen);
out.write(reinterpret_cast<const char*>(&year),sizeof(int));
out.write(reinterpret_cast<const char*>(&salary),sizeof(int));
}
void Personal::readFromFile(fstream& in)
{
in.read(SSN,9);
in.read(name,nameLen);
in.read(city,cityLen);
in.read(reinterpret_cast<char*>(&year),sizeof(int));
in.read(reinterpret_cast<char*>(&salary),sizeof(int));
}
void Personal::readKey()
{
char s[80];
cout << "Enter SSN: ";
cin.getline(s,80);
strncpy(SSN,s,9);
}
ostream& Personal::writeLegibly(ostream& out)
{
SSN[9] = name[nameLen] = city[cityLen] = '\0';
out << "SSN = " << SSN << ", name = " << name
<< ", city = " << city << ", year = " << year
<< ", salary = " << salary;
return out;
}
istream& Personal::readFromConsole(istream& in)
{
char s[80];
cout << "SSN: ";
in.getline(s,80);
strncpy(SSN,s,9);
cout << "Name: ";
in.getline(s,80);
strncpy(name,s,nameLen);
cout << "City: ";
in.getline(s,80);
strncpy(city,s,cityLen);
cout << "Birthyear: ";
in >> year;
cout << "Salary: ";
in >> salary;
in.getline(s,80); // get '\n'
return in;
}