#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class phonebook {
char name[80];
char areacode[4];
char prefix[4];
char num[5];
public:
phonebook() { };
phonebook(char *n, char *a, char *p, char *nm)
{
strcpy(name, n);
strcpy(areacode, a);
strcpy(prefix, p);
strcpy(num, nm);
}
friend ostream &operator<<(ostream &stream, phonebook o);
friend istream &operator>>(istream &stream, phonebook &o);
};
ostream &operator<<(ostream &stream, phonebook o)
{
stream << o.name << " ";
stream << "(" << o.areacode << ") ";
stream << o.prefix << "-";
stream << o.num << "\n";
return stream; // must return stream
}
istream &operator>>(istream &stream, phonebook &o)
{
cout << "Enter name: ";
stream >> o.name;
cout << "Enter area code: ";
stream >> o.areacode;
cout << "Enter prefix: ";
stream >> o.prefix;
cout << "Enter number: ";
stream >> o.num;
cout << "\n";
return stream;
}
int main()
{
phonebook a;
char c;
fstream pb("phone", ios::in | ios::out | ios::app);
if(!pb) {
cout << "Cannot open phone book file.\n";
return 1;
}
cin >> a;
cout << "Entry is: ";
cout << a; // show on screen
pb << a; // write to disk
char ch;
pb.seekg(0, ios::beg);
while(!pb.eof()) {
pb.get(ch);
if(!pb.eof()) cout << ch;
}
pb.clear(); // reset eof
cout << endl;
pb.close();
}
|