Using a existing text file and edi and save

How can I use my saved list to add new information in it and save it again?

I canĀ“t find any information on how to do.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  // Phonebook

/* Use this to create small registers of users in projects*/

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <sstream>

using namespace std;

class Phonebook
{
public:
	Phonebook() : name(""), number(0) {}
	Phonebook(const string &_name, int _number) : name(_name), number(_number) {}
	Phonebook(const Phonebook &rhs) : name(rhs.name), number(rhs.number) {}
	Phonebook operator=(const Phonebook &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		number = rhs.number;
		return *this;
		
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _number)
	{
		name = _name;
		number = _number;
	}
	
	const string &Name() const { return name; }
	int Number() const { return number; }

	private:
	string name;
	int number;
};

//Function Declarations
void fillPhonebook(vector<Phonebook>&myPhonebook);
//fillVector
//@param vector<Class>)& - the vector to be filled


//Function Declarations
void bubbleSort(vector<Phonebook>&myPhonebook);
// BubbleSort - Sorts vector
// @param myList - The vector to be sorted

void swap(Phonebook &p, Phonebook &q);
// Swap - Creates temporary variables for swapping Names

int i = 0;
int deleteFromVector;

int main()
{
	vector<Phonebook>myPhonebook;
	
	while(true)
	{
		cout << " - Press 0 for info about Phonebook Application" << endl;
		cout << " - Press 1 to add a person and number" << endl;
		cout << " - Press 2 to print list" << endl;
		cout << " - Press 3 to delete person and number" << endl;
		cout << " - Press 4 to save list" << endl;
		cout << " - Press 5 to load and print list" << endl;
		cout << " - Press 6 to quit" << endl;

		int choice;
		cin >> choice;

		cout << endl;
	
		switch(choice)
		{

			case 0:
			{
				cout << "******Information about Phonebook Application******\n" << endl;
				cout << "An application for small phonelists in projects\n" << endl;
				cout << "1 - Creates a new list and adds a person to it.\nYou can add new persons to this list by pressing 1.\n" << endl;
				cout << "2 - Prints list.\n" << endl;
				cout << "3 - Delete person from the list.\n" << endl;
				cout << "4 - Saves the list.\n" << endl;
				cout << "5 - Loads and print the old list from a file.\n" << endl;
				cout << "***************************************************\n" << endl;
			}
			break;

			case 1:
			{
				fillPhonebook(myPhonebook);
			}
			break;		
			case 2:
			{
				/* cout << "Print all names and numbers" << endl;
				printPhonebook(myPhonebook); */

				bubbleSort(myPhonebook);
				cout << "Sort order by age, youngest first:" << endl;
				for (unsigned int i = 0; i < myPhonebook.size(); i++)
				{
					cout << myPhonebook[i].Name() << " " << myPhonebook[i].Number() << endl;
				}

				cout << "\n" << endl;

				ofstream outFile;
					outFile.open("Phonebook.txt");
					for (unsigned int i = 0; i < myPhonebook.size(); i++)
					{
						outFile << myPhonebook[i].Name() << " " << myPhonebook[i].Number() << endl;
					}
					outFile.close();
			}
			break;		
			case 3:
			{
				int index;

				cout << "Which index do you want to delete? (1-10)" << endl;

				bubbleSort(myPhonebook);
				for (unsigned int i = 0; i < myPhonebook.size(); i++)
				{
					cout << i << " " << myPhonebook[i].Name() << " " << myPhonebook[i].Number() << endl;
				}

				cin >> index; 

				cout << "\n";

				myPhonebook.erase(myPhonebook.begin() + index);
      		}
			break;
			case 4:
			{
				ofstream outFile;
				outFile.open("Phonebook.txt");
				for (unsigned int i = 0; i < myPhonebook.size(); i++)
				{
					outFile << myPhonebook[i].Name() << " " << myPhonebook[i].Number() << endl;
				}
				outFile.close();
			}
			break;
			case 5:
			{
				string line;

				ifstream inFile ("Phonebook.txt");
				if (inFile.is_open())
				{
					while (getline (inFile,line) )
					{
						cout << line << '\n';
					}

					inFile.close();
				}
				else cout << "Unable to open file"; 
			}
			break;
		}
		if(choice==6)
			break;
	}

	return 0;
}

// Function Definitions
void fillPhonebook(vector<Phonebook>&myPhonebook)
{
		string name;
		int number;					

		// Flush makes sure that the buffer is cleared and the characters are written to their destination
		cout << "Enter persons name: " << flush << endl;
		
		// Enter values into vector
		cin >> name;
			
		cout << "Enter persons number: " << flush << endl;
		
		// Enter values into vector
		cin >> number;

		/*Phonebook Phonebook(name, number);
		myPhonebook.push_back(Phonebook);*/

		myPhonebook.push_back(Phonebook(name, number));

		cout << endl;

		
			
}

void bubbleSort(vector<Phonebook>&myPhonebook)
{
	// The outer loop, going through all list
    for(unsigned int i = 0; i < myPhonebook.size(); i++)
	{
		//Inner loop, going through element by element
		int nrLeft = myPhonebook.size() - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (myPhonebook[j].Name() > myPhonebook[j+1].Name()) // Compare the elements
			{
				swap(myPhonebook[j], myPhonebook[j+1]); 
			}
		}
	}
}

void swap(Phonebook &p, Phonebook &q) 
{ 
	Phonebook temp = p; 
	p = q;
	q = temp;
}


My quick suggestion is not closing the output file until the end of the program.
But that seems to easy and probably won't work.
Topic archived. No new replies allowed.