0% found this document useful (0 votes)
49 views10 pages

Address Book

This document describes a C++ program that implements a simple telephone directory or address book application. The program allows users to insert, retrieve, and delete contact records from a data file. It uses a TelRecord class to represent each contact, and stores the contacts in a std::set for easy retrieval and deletion. The main function handles command line arguments to perform queries, inserts, deletes and listings of the contact data.

Uploaded by

Tamere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views10 pages

Address Book

This document describes a C++ program that implements a simple telephone directory or address book application. The program allows users to insert, retrieve, and delete contact records from a data file. It uses a TelRecord class to represent each contact, and stores the contacts in a std::set for easy retrieval and deletion. The main function handles command line arguments to perform queries, inserts, deletes and listings of the contact data.

Uploaded by

Tamere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

sample application: telephone directory

1 tinf2$ tel fred "x2356 02-6124441" # insert data for fred


2 tinf2$ tel jane 092-6124441 # insert data for jane
3 tinf2$ tel fred # retrieve data for fred
fred
x2356 02-6124441
4 tinf2$ tel fred "" # delete fred
5 tinf2$ tel fred
"fred" not found
6 tinf2$ tel # show all data in the file
jane 092-6124441

tel.h
1
2
3
4
5

#ifndef TEL_H
#define TEL_H
// $Id: structuur2.tex,v 1.26 2008/09/02 11:51:15 dvermeir Exp
#include <string>
#include <iostream>

6
7
8
9
10
11
12
13
14
15
16
17

// associates a string info_ with a string name_


class TelRecord {
public:
TelRecord(const std::string& name="",
const std::string& info=""): name_(name), info_(info) {}
TelRecord(const TelRecord& r):
name_(r.name_), info_(r.info_) {}
// two records are the same if their names match
bool operator==(const TelRecord& r) const {
return name_ == r.name_;
}

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

// needed for std::set<TelRecord>


bool operator<(const TelRecord& r) const {
return name_ < r.name_;
}
friend std::ostream& operator<<(std::ostream& os,
const TelRecord& r);
friend std::istream& operator>>(std::istream& os,
TelRecord& r);
private:
// name_ is a unique key: no 2 records can
// have the same name_, name_ may not
// contain white space
std::string name_;
std::string info_;
};
#endif

tel.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

$Id: structuur2.tex,v 1.26 2008/09/02 11:51:15 dvermeir Exp


usage:
tel name
-- query: retrieve info for name
from database
tel name info -- insert: store/replace info for
name in database
tel name ""
-- delete: remove name from database
tel
-- list whole database
error return codes:
1
2
3
4

-----

query: not found


insert: cannot write database file
delete: not found
too many arguments

only 1 info string can be associated with a name


in the database

19
20
21
22

#include
#include
#include
#include

<iterator>
<fstream>
<set>
"tel.h"

23
24
25
26
27
28

std::ostream&
operator<<(std::ostream& os, const TelRecord& r) {
os << r.name_ << \t << r.info_ << std::endl;
return os;
}

29
30
31
32
33
34
35
36

std::istream&
operator>>(std::istream& is, TelRecord& r) {
is >> r.name_ ; // line starts with name
is.ignore(); // ignore \t following name
std::getline(is, r.info_); // rest of line is info
return is;
}

37
38
39
40

// note that to use set<T>, T needs a default and a


//
copy ctor as well as an operator<
typedef std::set<TelRecord> record_set;
typedef std::istream_iterator<TelRecord> record_input_it;

41
42

const char* const database_name = "tel.data";

43
44
45
46
47
48
49
50
51
52
53
54
55
56

int
main (int argc, char *argv[]) {
record_set dir;
{ // Load the database into a record_set
std::ifstream data_file(database_name);
if (data_file)
std::copy(
record_input_it(data_file),
record_input_it(),
std::inserter(dir, dir.begin())
);
}
// data_files is automatically closed by the ifstream destruc

57
58
59
60
61
62
63

switch (argc) {
// no arguments: just show all records on std::cout
case 1:
copy( dir.begin(), dir.end(),
std::ostream_iterator<TelRecord>(std::cout)
);
return 0;

64
65
66
67
68

// 1 argument: retrieve info associated with argv[1]


case 2:
{ // use the find() algo with dummy TelRecord with name_=
std::string key(argv[1]);
record_set::iterator r = dir.find(TelRecord(key, ""));

69
70
71
72
73
74
75
76
77
78
79
80
81

if (r!=dir.end()) {
// success: iterator points to found record
std::cout << *r; // output it
return 0;
}
else {
std::cerr << "\"" << key
<< "\" not found" << std::endl;
return 1;
}
}
break;

82
83
84
85
86

// 2 arguments, insert, replace or delete


case 3: {
std::string key(argv[1]);
std::string info(argv[2]);
TelRecord r(key, info);

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

if (info.size()==0) { // delete
if (dir.erase(r)!=1) {
// set::erase() returns number of deleted elements
std::cerr << "\"" << key
<< "\" not found; cannot erase" << std::endl;
return 3;
}
}
else { // insert or replace
std::pair<record_set::iterator, bool> pair =
dir.insert(r);
if (!pair.second) { // insert failed: duplicate key
dir.erase(pair.first); // erase,
dir.insert(r); // then insert again
}
}

// save to file after update


std::ofstream data_file(database_name);
if (!data_file) {
std::cerr << "Cannot open \"" << database_name
<< "\" for writing" << std::endl;
return 2;
}
std::copy(dir.begin(), dir.end(),
std::ostream_iterator<TelRecord>(data_file));
return 0;
}
break;
default:
std::cerr << "Usage: " << argv[0]
<< " [name [info|\"\"]] " << std::endl;
return 4;
break;
}
return 0;

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

You might also like