Saving and Writing to a txt file

I'm trying to create an inventory program that you can save the data and open it again. But every time I run the program again my txt file empties so it's blank. Is there some way to save that file so I can retrieve the same data at different time?

I'm sorry the code is so long but 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
217
218
219
220
221
222
223
224
225
226
  
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;

// define a data structure
struct Inventory
{
  string name;   // the name of the stamp
  int year;       // the year the stamp was made
  int cost;   // the cost of stamp
};

// const for the max size of the record array
const int MAX= 10;

// define functions
void getRecords(Inventory list[], int& size);
void editRecord(Inventory list[], int size);
void findRecord(Inventory list[], int size);
void sortRecords(Inventory list [], int size);
void displayRecords(const Inventory list[], int size);
void openFile(Inventory s[], int size);
void saveFile(ofstream& fout, Inventory s[], int size);
char getMenuResponse();


int main(int argc, char *argv[])
{
	ofstream fout("Storage.txt", ios::app);
	ifstream fin("Storage.txt", ios::app);
	Inventory recList[MAX];
	int numOfRecs = 0;
	bool run = true;
	do
	{
		cout << "Your stamp collection - " << numOfRecs << " items in stock" << endl;
  		switch ( getMenuResponse() ) 
  		{
  			case '1': getRecords(recList, numOfRecs); break;
  			case '2': editRecord(recList, numOfRecs); break;
	  		case '3': findRecord(recList, numOfRecs); break;
  			case '4': sortRecords(recList, numOfRecs); break;
			case '5': displayRecords(recList, numOfRecs); break;
  			case '6': openFile(recList, numOfRecs); break;
  			case '7': saveFile(fout, recList, numOfRecs); break;
 	 		case '8': run = false; break;
  			default : cout << "Please choose an option from the menu" << endl;
		}
	}
	while (run);
	cout << endl << "Good bye" << endl;
  
  // system("PAUSE"); // Program exits immediatly upon "Quit" if commented out
	return EXIT_SUCCESS;
}


void getRecords(Inventory s[], int& size)
{
	Inventory tmp;
	cout<< "Enter your records (quit to stop)"<<endl;
	if (size<MAX)
	{
		for (int i=size; i<MAX; i++)
		{
		string response;
		stringstream ss;
		cout<< "Name:   ";
		cin>>tmp.name;
		if (tmp.name=="quit")
			break;
		cout<< "Year:   ";
		cin>>response;
		ss<<response;
		ss>> tmp.year;
		ss.str(" ");
		ss.clear();
		cout<< "Worth:  ";
		cin>>response;
		ss<<response;
		ss>> tmp.cost;
		cout<< "Store record in inventory? (yes/no):  ";
		cin>> response;
		if (toupper(response=="yes"))
			s[size++]=tmp;
		else
		{
			cout<<"Record not added to inventory"<< endl;
			break;
		}
		}
	}
	else
		cout<< "Inventory is full, cannot enter more items";
    system("cls");
}

void editRecord(Inventory s[], int size)
{
	Inventory tmp;
	string response;
	stringstream ss;
	int i;
	cout<< "Which entry would you like to edit?"<< endl;
	cout<< "Record #:  ";
	cin>> i;
	cout<<s[i].name<<endl;
	cout<<s[i].year<<endl;
	cout<<s[i].cost<<endl;
	cout<< "Name:   ";
	cin>> tmp.name;
	cout<< "Year:   ";
	cin>>response;
	ss<<response;
	ss>> tmp.year;
	cout<< "Worth:  ";
	cin>>response;
	ss<<response;
	ss>> tmp.cost;
	s[i]=tmp;
    system("cls");
}

void findRecord(Inventory s[], int size)
{	
	char t[256];
	cout<< "Which name are you looking for? "<< endl;
	cout<< "Name: ";
	cin.getline(t, 256, '\n');
	for (int i=0; i<MAX;++i)
	{
		if (t==s[i].name)
		{
			cout<< "This record is entry "<< i<< endl;
			break;
		}
	}
    system("PAUSE");
    system("cls");	
}

void sortRecords(Inventory s[], int size)
{
	for (int i=1; i<size; i++)
	{
		for (int j=0; j<size-i; j++)
		if( s[j].name>s[j+1].name)
			swap(s[j].name, s[j+1].name);
	}
    system("cls");	
}

void displayRecords(const Inventory s[], int size)
{
	cout<<"Stamp Name "<<setw(24)<<"Production Year"
		<<setw(10)<<"Worth" <<endl;
	cout<< endl;
	cout<<left;
	for (int i=0; i<size; i++)
	cout<< setw(20)<<s[i].name<< setw(20)<<left<<s[i].year
		<<setw(20)<<left<<s[i].cost<<endl;
	system("PAUSE");
    system("cls");
}


 
void saveFile(ofstream& fout, Inventory s[], int size)
{
	fout.open("Storage.txt", ios::app);
	for(int i=0; i<size; i++)
	{
		fout<<s[i].name<<';';
		fout<<s[i].year<<';';
		fout<<s[i].cost;
		if(i<size-1)
			fout<<endl;
	}
	fout.close();
	system("PAUSE");
	system("cls");
}


void openFile(Inventory s[], int size)
{
    string rec;
	ifstream fin;
	stringstream ss;
	fin.open ("Storage.txt", ios::app);
    int i=0;
	while(!fin.eof()) // To get you all the lines.
    {
	    getline(fin,rec, ';'); // Saves the line in a string
	        s[i].name=rec;
		getline(fin, rec, ';');
		ss.str("");
		ss.clear();
		ss<<rec;
		ss>>s[i].year;
		getline(fin, rec);
		ss.str("");
		ss.clear();
		ss<<rec;
		ss>>s[i++].cost;	
        }
	fin.close();
	system ("pause");
}
char getMenuResponse()

{
	char response;
	cout << endl << "Welcome, please choose an option:" << endl
		 << "1-Enter new record 2-Edit a record 3- Find a record 4-Sort Records" << endl
		 << "5- Display records 6- Open file 7- Save file 8- Quit"<< endl;
	cin >> response;
	cin.ignore(256, '\n');	

	return response;
}

see 13.6 and 13.7 http://www.learncpp.com/
I thought I had used ios::app. Is there something more I need to include?
follow the link I've given. and use fstream object to read and write simultaneously in single file.

try small test case first. if you success then apply it to existing program.
Topic archived. No new replies allowed.