How to overrite a .txt file

I was wondering if anyone can give me tips on how to overwrite/update a .txt file. I give the user the option to add/delete data in a .txt file, and I want to be able to over write the .txt file with the changes they make. This is what I have, and it's not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void writeData(int& count)
{
	ofstream aptInfo("RENTALS.TXT");

	if (aptInfo.is_open())
	{
		for (int i = 0; i < count; i++)
		{
			aptInfo << array1[i].phoneNum << " " << array1[i].price << " " << array1[i].vacancy << endl;
			//writes data to file
		}
	}
	else
		cout << "Could not open file. \n";
}
Here is my entire program, if that helps.

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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
#include <iomanip>
#include <cstdlib>
using namespace std;

const int MAX = 500;
const string APT_INFO = "RENTALS.TXT";
void readFile(int& count);
char newFile();
void userOption(int& count);
void addNew(int& count);
void deleteList(int& count);
void writeData(int& count);

struct houses{
	string phoneNum;
	double price;
	string vacancy;
};

houses array1[MAX];


int main()
{
	char choiceOne; // Whether of not the user wants to load file
	int count = 0;
	
	cout << "This program will give a list of homes to the user.  It will provide the "
		"user with the phone number, price, and whether or not the home is rented."
		<< endl << endl;

	choiceOne = newFile();

	if (choiceOne == 'Y')
		readFile(count);
	userOption(count);

	
	cout << endl << endl;
	system("PAUSE");
	return(0);
}

char newFile()
{
	char userLoad;
	
	cout << "Load existing data from a file (Y/N)? ";
	cin >> userLoad;
	cout << endl << endl;

	userLoad = toupper(userLoad);

	while ((userLoad != 'Y') && (userLoad != 'N'))
	{
		cout << "Invalid entry, please enter a 'Y' or a 'N': " << endl << endl;
		cout << "Load existing data from a file (Y/N)? ";
		cin >> userLoad;
		cout << endl << endl;
	}

	return(userLoad);
}

void readFile(int& count)
{
	ifstream aptInfo;
	double cost;
	string phNum;
	string avail;
	int i = 0;

	aptInfo.open(APT_INFO.c_str()); //opens the file for reading

	if (aptInfo) //if the file opens successfully
	{
		aptInfo >> phNum >> cost >> avail; //priming read

		while ((aptInfo) && (count <= MAX))
		{
			i++;

			if (avail == "0")
				avail = "Not Rented";
			else
				avail = "Rented";

			array1[i].phoneNum = phNum;
			array1[i].price = cost;
			array1[i].vacancy = avail;

			count = i;

			aptInfo >> phNum >> cost >> avail;
		}
	}


	if (count == MAX)
	{
		cout << "There are too many records in the file, all further"
			"records will be ignored." << endl;
	}
}

void userOption(int& count)
{
	char choice = 'A';
	
	cout << "Phone Number\t\tMonthly Rent\t\tStatus\n"
		"-------------\t\t------------\t\t---------\n";
	for (int i = 1; i <= count; i++)
	{
		cout << array1[i].phoneNum << "\t\t" << array1[i].price << "\t\t\t" << array1[i].vacancy << endl;
	}


	while ((choice == 'A') || (choice == 'B'))
	{
		cout << "\nMENU \n ---------- \n A. Add New Home \n B. Delete a Rental \n C. Exit \n"
			"What is your choice? ";
		cin >> choice;
		cout << endl << endl;
		choice = toupper(choice);
		
		if (choice == 'C')
		{
			exit(0);
		}
		if (choice == 'A')
		{
			addNew(count);
			writeData(count);
			readFile(count);
		}
		else if (choice == 'B')
		{
			deleteList(count);
			writeData(count);
			readFile(count);
		}

	};
}

void addNew(int& count)
{
	string phNum;
	char rentStatus;
	double rent;
	string convert;

	cout << "Please enter the phone number associated with the apartment in the formate xxx-xxx-xxxx: ";
	cin >> phNum;
	cout << endl;

	count++;
	array1[count].phoneNum = phNum;

	cout << "Please enter how much the rent is: ";
	cin >> rent;
	cout << endl;

	while (rent <= 0)
	{
		cout << "Invalid entry. Rent must be a positive number: ";
		cin >> rent;
		cout << endl;
	}
	array1[count].price = rent;

	cout << "Is the apartment rented (Y/N): ";
	cin >> rentStatus;
	rentStatus == toupper(rentStatus);

	while ((rentStatus != 'Y') && (rentStatus != 'N'))
	{
		cout << "Invalid entry.  Enter a 'Y' or a 'N': ";
		cin >> rentStatus;
		rentStatus = toupper(rentStatus);
	}

	if (rentStatus == 'Y')
		convert = '1';
	else
		convert = '0';
	cout << endl;
	array1[count].vacancy = convert;

}

void deleteList(int& count) //write code to delete a listing next
{
	int placeFound;               // index where found
	string itemToDel;
	bool deleted;

	deleted = false;              // item not found and deleted yet

	cout << "What is the phone number for the listing you want to delete? ";
	cin >> itemToDel;
	cout << endl;

	// Find where itemToDel is in list
	placeFound = 0;                             // Start search at first index

	while ((placeFound < count) &&           // While still values to search
		(array1[placeFound].phoneNum != itemToDel))     // and value not found
	{
		placeFound++;                        // increment index

		if (placeFound == count)
		{
			cout << "Phone number not found.  Please try again: ";
			cin >> itemToDel;
			cout << endl;
			placeFound = 0;
		}
	}

	//If itemToDel was Found, delete it
	if (placeFound < count)
	{

		// Move all values below itemToDel UP one cell
		for (int num = placeFound + 1; num < count; num++)
		{
			array1[num - 1].phoneNum = array1[num].phoneNum;
			array1[num - 1].price = array1[num - 1].price;
			array1[num - 1].vacancy = array1[num - 1].vacancy;
		}

		// Decrement list size
		count--;

		// Zero out deleted last item
		array1[count].phoneNum = "";	// Optional
		array1[count].price = 0;
		array1[count].vacancy = "";

		deleted = true;
	}
}

void writeData(int& count)
{
	ofstream aptInfo("RENTALS.TXT");

	if (aptInfo.is_open())
	{
		for (int i = 0; i < count; i++)
		{
			aptInfo << array1[i].phoneNum << " " << array1[i].price << " " << array1[i].vacancy << endl;
			//writes data to file
		}
	}
	else
		cout << "Could not open file. \n";
}
Are you saying it doesn't overwrite everything in the text file ?

What exactly do you mean isn't working ?
can you explain the int& for me in line 250?
Opening a file in truncate mode may be what you want?
std::fstream file("something.txt", std::fstream::trunc);
He's opening with an ofstream, so the 'trunc' is automatic.
SamuelAdams, I am trying to enable the user to edit the txt file by adding, or deleting data. How it should work is the I load the data into array1[] from the .txt file, the user deletes/adds data into the array1[], then I would like to be able to write array1[] into the same txt file that is originally loaded into the array.

The int& in line 250 is there because I thought I would need it. It should be just a int.

The program should be able to loop through until the user wants to exit. So the user should be able to add/delete listings over and over, until he/she wants to close the program.
1. Load file into array and close file
2. Modify array as requested by user
3. Write array back to an std::ofstream file and close file
What part is causing your trouble?
Ok, I figured it out. I didn't realize it was as easy as kevinkjt explained. It's working now! Thank you guys.
Topic archived. No new replies allowed.