File i/o and databasing

I am trying to write a databasing program to store information about people, such as DOB, address, etc.
What I am trying to do is write all of these entries into a file, and then use the file to read off of.

My question is how to index the file to make it search-able in order to delete specific entries, and also how to write different entries on different lines.

Here is my code thus so far (adding and deleting are functions of the class people)
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
struct address{
	int numbers, zip;
	string street, city, state;
};
	
class people {
	public:
	// all the fields of data to record
	string name;
	address adr;
	string number;
	string DOB;

	// all the functions of the class
	void add();
	void del();
	void print_all(); //the whole phonebook
	void print_info(); //the individual's info
	void print_date();
	void print_num();
};
inline void people::add()
{
	ofstream output;

	output.open("data.txt");

	cout << "Please enter the person's info:\n"
	<< "name:\n" ;
	cin >> name;
	cout << "address:\n";
	cin >> adr.numbers >> adr.street ;
	cout << "city:\n";
	cin >> adr.city;
	cout << "state:\n";
	cin >>adr.state;
	cout <<"zip:\n";
	cin >> adr.zip;

	cout << "phone number:\n";
	cin >> number;

	cout << "date of birth:\n";
	cin >> DOB;
	cout << " ";
	output << name << " "<<adr.numbers <<" " << adr.street<< " " <<adr.city << " "		<<adr.state << " " << adr.zip << " "<< DOB <<"\n"; 
	
}


int main() {

cout << "Make a selection from these menu items:\n"
	<< "1. Add People   2. Delete People   3. Print the phonebook\n"
	<< "4. Print an individual   5. Print a birthdate  6. Print a phone number\n"
	<< "7. Quit    " << "Please enter your choice:\n";

int choice;

people pep;

while ( choice != 7){
cin >> choice;

	switch (choice)
	{
		case 1:		
		pep.add(); 	
		break;

		case 2:
	        pep.del();		
		break;

etc.
Can anyone help me?

~Thanks
Well, you could either use a vector or a map to store the data from the file. A vector is more general purpose (you just store the strings), and a map is more "searchable" since it uses a "key". However, if you want the person to be able to input any search term (name, address, etc) I would use a vector.

Then, just go through the vector (or map) and use the string functions to find the information you are looking for.
Can you tell me how to copy all of the file's contents to the map, or even the structure of it?

I do not really understand how one searches through a map after reading this: http://www.cplusplus.com/reference/stl/map/

Also, can you tell me how to write to the end of the file, as opposed to writing over the information already in it as my code does now, or do I have to copy the entire file to memory, and then out put the file at the end of my operations on it?


~Thanks
Topic archived. No new replies allowed.