Input file deleted whenever I run the program

I am trying to make a program that calls an input and output function to a file. My goal is that a user could change the integers stored in the stores array, then later if they used the program again they could read it off of the same file. I have the output working fine, but even with existing data anytime I run the program it deletes everything in the file so there is nothing to read. I do have it set up to delete the file and make a new one whenever I run the output, but if that function is never called up I don't understand why it clears all the text. Any help would be appreciated.

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int SIZE = 5;

int stores[SIZE];
string food[SIZE] = { "Milk", "Eggs", "Beef", "Poultry", "Flour" };
const int milk_Choice = 1;
const int eggs_Choice = 2;
const int beef_Choice = 3;
const int poultry_Choice = 4;
const int flour_Choice = 5;
const int list_Choice = 6;
const int math_Choice = 7;
const int output_Choice = 8;
const int input_Choice = 9;
const int end_Choice = 10;


void options();
void addFunction(int stores[]);
void showMilk(int stores[], string food[]);
void showEggs(int stores[], string food[]);
void showBeef(int stores[], string food[]);
void showPoultry(int stores[], string food[]);
void showFlour(int stores[], string food[]);
void fromFile(int stores[], ifstream &inFile);
void toFile(int stores[], ofstream &outFile);
void mathTotal(int stores[]);


int main()
{
	int choice;
	ofstream outFile("Total_List.txt");
	ifstream inFile("Total_List.txt");

	cout << "This program will allow you to store and view common foodstuffs in stock" << endl;
	cout << endl;
	options();
	cin >> choice;

	do
	{
		if (choice > 10 || choice < 0)
		{
			cout << "Please enter an appropriate choice" << endl;
			cout << endl;
			options();
			cin >> choice;
		}
		else if (choice == milk_Choice)
		{
			showMilk(stores, food);
			options();
			cin >> choice;
		}
		else if (choice == eggs_Choice)
		{
			showEggs(stores, food);
			options();
			cin >> choice;
		}
		else if (choice == beef_Choice)
		{
			showBeef(stores, food);
			options();
			cin >> choice;
		}
		else if (choice == poultry_Choice)
		{
			showPoultry(stores, food);
			options();
			cin >> choice;
		}
		else if (choice == flour_Choice)
		{
			showFlour(stores, food);
			options();
			cin >> choice;
		}
		else if (choice == list_Choice)
		{
			addFunction(stores);
			options();
			cin >> choice;
		}
		else if (choice == math_Choice)
		{
			mathTotal(stores);
			options();
			cin >> choice;

		}
		else if (choice == output_Choice)
		{
			toFile(stores, outFile);
			options();
			cin >> choice;
		}
		else if (choice == input_Choice)
		{
			fromFile(stores, inFile);
			options();
			cin >> choice;
		}
		else if (choice == end_Choice)
		return 0;			

		} while (choice == milk_Choice || choice == eggs_Choice || choice == beef_Choice || choice == poultry_Choice || choice == flour_Choice
			|| choice == list_Choice || choice == list_Choice || choice == math_Choice || choice == output_Choice || choice == input_Choice);

		system("pause");
	
}




void options()
{
	
	cout << "Please select from the following options" << endl;
	cout << "1. View Milk(Gallons)" << endl;
	cout << "2. View Eggs(Dozens)" << endl;
	cout << "3. View Beef(lbs.)" << endl;
	cout << "4. View Poultry(lbs.)" << endl;
	cout << "5. View Flour(lbs.)" << endl;
	cout << "6. Change the amount of items" << endl;
	cout << "7. Total amount of all items" << endl;
	cout << "8. Send information to file" << endl;
	cout << "9. Retrieve information from file" << endl;
	cout << "10. End Program" << endl;

}


void addFunction(int stores[]) // The various sum functions
{

	for (int i = 0; i < SIZE; i++)
	{
		cout << "Enter how many " << food[i] << " you wish to track." << endl;
		cin >> stores[i];
		cout << endl;
	}

}

void showMilk(int stores[], string food[])
{
	cout << "You have " << stores[0] << " gallons of "<< food[0] << endl;
	cout << endl;
}

void showEggs(int stores[], string food[])
{
	cout << "You have " << stores[1] << " dozen "<< food[1] << endl;
	cout << endl;
}

void showBeef(int stores[], string food[])
{
	cout << "You have " << stores[2] << " pounds of "<< food[2] << endl;
	cout << endl;
}

void showPoultry(int stores[], string Food[])
{
	cout << "You have " << stores[3] << " pounds of "<<food[3] << endl;
	cout << endl;
}

void showFlour(int stores[], string food[])
{
	cout << "You have " << stores[4] << " pounds of "<<food[4] << endl;
	cout << endl;
}

void toFile(int stores[], ofstream &outFile)
{
	remove("Total_List.txt");
	for (int i = 0; i < SIZE; i++)
		outFile << stores[i] << endl;
	outFile.close();

	cout << " The quantity of each item has been listed onto the file" << endl;
	cout << endl;
	

}

void fromFile(int stores[], ifstream &inFile)
{
	inFile.open("Total_List.txt");
	for (int i = 0; i < SIZE; i++)
		inFile >> stores[i];
	inFile.close();
	cout << " Your inventory has been updated from the file" << endl;	
	cout << endl;

}

void mathTotal(int stores[])
{
	int grandTotal = 0;
	grandTotal = stores[0] + stores[1] + stores[2] + stores[3] + stores[4];
	cout << " The total amount of food items you have is " << grandTotal << endl;
	cout << endl;

}

closed account (E0p9LyTq)
Why is your file disappearing? Line 185: remove("Total_List.txt");
http://www.cplusplus.com/reference/cstdio/remove/
But why is it deleting it if I don't call the function that has it? Is there a better way to do it? I originally added that command because it kept adding more integers instead of replacing the existing ones. I read something about truncating but I didn't fully understand it.
closed account (E0p9LyTq)
http://www.cplusplus.com/reference/fstream/ofstream/open/

Instead of remove(), open the file: outFile.open ("test.txt", std::ofstream::out | std::ofstream::trunc);
That worked great, thanks for the help.
Topic archived. No new replies allowed.