By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

Random Access File

P: 3
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.
Here is the .cpp

//personal.cpp//

#include "personal.h"

Personal::Personal() : nameLen(10), cityLen(10) {
name = new char[nameLen+1];
city = new char[cityLen+1];
}

Personal::Personal(char *ssn, 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(in t));
}

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;
}
Dec 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: 3
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.
Here is the .cpp

//personal.cpp//

#include "personal.h"

Personal::Personal() : nameLen(10), cityLen(10) {
name = new char[nameLen+1];
city = new char[cityLen+1];
}

Personal::Personal(char *ssn, 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(in t));
}

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;
}
Posted wrong file

#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 isNull();
int size() const

{
return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
}
bool operator==(const Personal& pr) const
{
return strncmp(pr.SSN,SSN,9) == 0;
}

Personal::Personal(char* = isNull)
{
isNull = new record;
writeNullToFile();
return;
}


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
Dec 23 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.