Editing text file in c++

Here is the problem.

PROGRAMMING PROBLEM You are requested to create an inventory control application for a drink stall (give a name to the stall). The application is used to show the list of drinks the stall sells, how many of each item on‐hand and the cost of each item. The records are to be saved and retrieved to/from a binary file called “drinks.txt”. For each drink in the stall, your program is to keep the information as a Record Structure called DrinkRecord. The structure should at least consist of the following: (1) Item Number (2) Item Name (3) Quantity (4) Unit Price

Your application should have the following features: (a) Display a drink information (b) Add a new drink information (c) Update a new drink information (d) Delete a drink information (e) Display all drink information

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
265
266
267
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stdlib.h>
#include <iomanip>
#include <stdexcept>
#include <cstdio>
#include <sstream>
#include <iostream>
using namespace std;

int option, no, quantity, n;
char opt;
double price;
string answer, name;



void record_edit()
{
	vector<vector<double> > contents;

	/* read file */
	{
		ifstream inFile("Drinks.txt");
		for (string line; inFile; getline(inFile, line)) {
			stringstream line_stream(line);
			vector<double> row;
			copy(istream_iterator<double>(line_stream), istream_iterator<double>(),
				back_inserter(row));
			contents.push_back(row);
		}
	}

	/* process file */
	unsigned int n = contents.size();
	for (unsigned int i = 0; i < n; ++i) {
		vector<double> row(5, 0.);
		bool add_row = false;
		if (contents[i].size() >= 5) {
			for (unsigned int j = 3; j<4; ++j) {
				double value = contents[i][j];
				contents[i][j] = 0.;
				if (value > 0) {
					add_row = true;
					row[j - 3] = value;
				}
			}
			if (add_row == true) {
				contents.push_back(row);
			}
		}
	}
	
	/* write file */
	for (unsigned int i = 0; i < contents.size(); ++i) {
		copy(contents[i].begin(), contents[i].end(), ostream_iterator<double>(cout, " "));
		cout << endl;
	}
	
}


void record_delete()
{
	vector<string> records;
	string line;
	ifstream records_input("Drinks.txt"); // create a file stream and open the file for reading
	while (getline(records_input, line)) // read all the lines from the file and store in vector records
	{
		records.push_back(line);
	}
	records_input.close(); // close the file
	size_t recno = records.size(); // get the number of lines read
	cout << "ItemNo" << "   " << "ItemName" << "   " << "Quantity" << "   " << "Unit Price" << endl;
	for (size_t i = 0; i < recno; ++i) // display all the lines read
	{
		cout << i << ": " << records[i] << endl;
	}
	size_t recno1 = 0;
	cout << "Which drink would you like to delete?" << endl;
	cin >> recno1; // get the user to pick a record
	if (recno1 < 0 || recno1 >= recno) // validate their choice
	{
		cout << "Invalid choice" << endl;
	}
	else
	{
		cout << "\nYou have chosen to delete record \n" << recno1 << endl;
		records.erase(records.begin() + recno1); // delete users choice from the records vector
		recno = records.size(); // the size of the vector will change
	}
	ofstream records_output("Drinks.txt"); // create a file stream and open the file for output
	cout << "\n";
	cout << "ItemNo" << "   " << "ItemName" << "   " << "Quantity" << "   " << "Unit Price" << endl;
	cout << "\n";
	for (size_t i = 0; i < recno; ++i) // write out the new records to screen
	{
		cout << i << ": " << records[i] << endl;
		records_output << records[i] << endl;// also overwrite the original file with the new records
	}
	records_output.close(); // close the file

}

void record_search()
{
	ifstream FileSearch;

	{
		string line;
		string letters;
		ifstream readSearch;

		cout << "Search for word: ";
		cin >> letters;
		"\n";
		FileSearch.open("Drinks.txt");
		if (FileSearch.is_open())
		{

			while (getline(FileSearch, line)) {
				if (line.find(letters) != string::npos) {
					cout << "\n";
					cout << "ItemNo" << "   " << "ItemName" << "   " << "Quantity" << "   " << "Unit Price" << endl;
					cout << "\n";
					cout << line << endl;
				}
			}
			cout << "\n";
			cout << letters << " not found!" << endl;
			cout << "\n";
		}
	}

}

void record_initialize()
{
	vector<string> record;

	cout << "\n";
	record.push_back("3"   "	 ""Ribena"   "   	""7"   "        ""2.2");
	record.push_back("17"   "	 ""Coffee"   "		""76"  "	 ""1.5");
	record.push_back("24"   "	 ""Tea"      "		""21"	 "	 ""1.1");
	record.push_back("39"   "	 ""Sprite"   "		""3"   "	 ""1.7");
	record.push_back("56"   "	 ""Cola"     "		""18"  "	 ""1.7");
	record.push_back("68"   "	 ""Orange"   "		""106" "	 ""2.2");
	record.push_back("77"   "	 ""Lime"     "		""11"  "	 ""2.1");
	record.push_back("86"   "	 ""Grape"    " 		""34"  "	 ""2.3");
	cout << "\n";

	ofstream output_file("Drinks.txt");
	ostream_iterator<string> output_iterator(output_file, "\n");
	copy(record.begin(), record.end(), output_iterator);

}

void record_add(){

	cout << "\n";
	cout << "Please enter the item no.: ";
	cin >> no;
	cout << "\n";
	cout << "Please enter name of drink: ";
	cin >> name;
	cout << "\n";
	cout << "Pleae enter the quantity: ";
	cin >> quantity;
	cout << "\n";
	cout << "Pleae enter the unit price: ";
	cin >> price;
	cout << "\n";


	ofstream myfile;
	myfile.open("Drinks.txt", ios::app | ios::out);

	cout << "\n";
	myfile << no<<   "	 "<<name<<   "   	"<<quantity<<   "        "<<price<< endl;
	myfile.close();



}

void record_view(){
	cout << "\n";
	cout << "ItemNo" << "   " << "ItemName" << "   " << "Quantity" << "   " << "Unit Price" << endl;
	cout << "\n";

	string getcontent;
	ifstream openfile("Drinks.txt");
	if (openfile.is_open())
	{
		while (!openfile.eof())
		{
			getline(openfile, getcontent);

			cout << getcontent << endl;

		}
	}


}

int main()
{

	record_initialize();

	do //do-while loop starts here.that display menu again and again until user select to exit program
	{

		//Displaying Options for the menu
		cout << "\n";
		cout << "***MAIN MENU***\n\n" << endl;
		cout << "1) Add Drink " << endl;
		cout << "2) Search Drink" << endl;
		cout << "3) Delete Drink" << endl;
		cout << "4) View Drinks" << endl;
		cout << "5) Edit Drink " << endl;
		cout << "6) Exit Program " << endl;
		//Prompting user to enter an option according to menu
		cout << "\nPlease select an option : ";
		cin >> option;  // taking option value as input and saving in variable "option"
		cout << "\n\n";
		if (option == 1) // Checking if user selected option 1
		{
			record_add();
		}
		else if (option == 2) // Checking if user selected option 2
		{
			record_search();
		}
		else if (option == 3) // Checking if user selected option 3
		{
			record_delete();
		}
		else if (option == 4) // Checking if user selected option 3
		{

			record_view();

		}

		else if (option == 5) // Checking if user selected option 3
		{
			record_edit();
		}

		else if (option == 6) // Checking if user selected option 4
		{
			cout << "\nTerminating Program\n" << endl;
		}
		else //if user has entered invalid choice (other than 1,2,3 or 4)
		{
			//Displaying error message
			cout << "\a";// outputs bell sound 
			cout << "Invalid Option entered" << endl;
		}
	} while (option != 6); //condition of do-while loop
}


Everything works fine with minor issues, but I'm having trouble with the editing function. I have tried everything, but I cant seem to edit the drink information. I need to be able to search for a specific drink then be able to edit its contents and print the new contents to the file. Could anyone please help me with the codes. Also, I have not created a structure yet. I will do that as soon as possible.

Thanks
Last edited on
One of the first things I see is this:

The records are to be saved and retrieved to/from a binary file called “drinks.txt”.

But when I look at your code I don't see where you're opening the files in binary mode.

Then then next thing I see is this:

For each drink in the stall, your program is to keep the information as a Record Structure called DrinkRecord.

But when I look at your code I don't see any such structure defined.

Then I see this:
1
2
3
4
5
6
using namespace std;

int option, no, quantity, n;
char opt;
double price;
string answer, name;


Global variables are a very bad practice and should be avoided when ever possible.


Lastly I see this:

Everything works fine with minor issues, but I'm having trouble with the editing function.


You can't "edit" text files, but if you were using binary files with fixed sized records the file could be edited, deleting records is a little more difficult but it is solvable.
Topic archived. No new replies allowed.