Changing a .dat File

Hello all,

I am working on an appointment planner, and I need to write a function to change an appointment. Each appointment has an ID. The user inputs the ID and what they want to change, and this then overwrites that area in the appointment. However, I can't get it to actually overwrite the old appointment. Everything seems to work except the edit_line function.

Here is the code so far:


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
268
269
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <strstream>
#include <iomanip>

using namespace std;

//initializing all functions and structures
int insert_newappointment(); string welcome_screen(); struct Date; struct Appointment; void show_all(); int check_id(int ID);



/*
==================
 Work
==================
*/

void edit_line()
{
	string id, appwith, day, month, year, starttime, duration, location;
	string test_id;
	cout << "Enter the appointment number to be edited: ";
	cin >> test_id;
	string change;
	cout << "Enter the attribute to be changed: (w for with, d for date, t for timing, l for location): ";
	cin >> change;
	fstream appointment_file;
	fstream new_file;
	new_file.open("test.dat");
	appointment_file.open("Appointments.dat");
	string line;
	cout << line << endl;
	while(getline(appointment_file, line))
	{

		istringstream isstream (line);
		ostringstream test (line);
 
		getline(isstream,id,';');
		if(test_id == id)
		{
			cout << "YAY!" << endl;
			getline(isstream,appwith,';');
			cout << appwith;
			appwith = "test";
			getline(isstream,day,';');
			getline(isstream,month,';');
			getline(isstream,year,';');
			getline(isstream,starttime,';');
			getline(isstream,duration,';');
			getline(isstream,location,';');
			test << id << appwith;
			string output = test.str();
			cout << output;
			new_file >> line;
		}
	}	
	system("pause");



}


/*
==================
End Work
==================
*/



//MAIN FUNCTION
int main()
{
	int quit;
	string line;

	do
	{
		string option_selected = welcome_screen();

		if(option_selected == "I" || option_selected == "i")
		{
			int keep_going=0;
			do
			{
				keep_going=insert_newappointment();
			}
			while(keep_going==1);
			quit=0;
		}
		else if (option_selected == "C" || option_selected == "c")
		{
			edit_line();
			quit=0;
		}
		else if (option_selected == "S" || option_selected == "s")
		{
			show_all();
			quit=0;
		}
		else if (option_selected == "Q" || option_selected == "q")
		{
			cout<<"Thank you for using the Appointments Planner!\n";
			quit=1;
		}
		else
		{
			cout<< "Option inputted not valid. Please try again.";
			quit=0;
		}
	}
	while(quit==0);

	system("pause");

	return 0;

}

//STRUCTURES AND FUNCTIONS

string welcome_screen()
{
		string choice;
		cout<< "************* Welcome to the Appointments Planner *************\n\n";
		cout<< "Please Select from one of the following options: \n";
		cout<< "\t I - to insert new appointment info\n";
		cout<< "\t C - to change the data for a particular existing appoint\n";
		cout<< "\t S - to show all the appointments\n";
		cout<< "\t Q - to quit this application\n";
		cout<< "Please enter one of the above letters: ";
		cin>> choice;
		cout<<endl;
		return choice;
}

struct Date
{
	string Day;
	string Month;
	string Year;
};

struct Appointment
{
	int appointmentID;
	string appointmentWith;
	Date appointmentDate;
	int appointmentStartTime;
	int appointmentDuration;
	string appointmentLocation;
};

int insert_newappointment()
{
	fstream appointment_file;
	appointment_file.open("Appointments.dat", ios::app);
	string line;
	int repeat=0;

	cout<< "Information regarding the new appointment: \n\n";

	Appointment file;
	Appointment* pointer;
	pointer=&file;

	do
	{
		cout<< "Enter the appointmentID: ";
		cin>>file.appointmentID;
		getline (cin, line);
		(stringstream) line>>pointer->appointmentID;
	}
	while(check_id(file.appointmentID)==1);


	cout<<"\nScheduled with: ";
	getline (cin, pointer->appointmentWith);

	cout<<"\nEnter the date of the appointment:";
	cout<<"\n    Day(DD): ";
	getline (cin, pointer->appointmentDate.Day);
	cout<<"\n    Month(MM): ";
	getline (cin, pointer->appointmentDate.Month);
	cout<<"\n    Year(YYYY):";
	getline (cin, pointer->appointmentDate.Year);

	cout<<"\nEnter the start time of appointment: ";
	getline (cin, line);
	(stringstream) line>>pointer->appointmentStartTime;

	cout<<"\nEnter the duration of appointment (in minutes): ";
	getline (cin, line);
	(stringstream) line>>pointer->appointmentDuration;

	cout<<"\nEnter the location of appointment: ";
	getline (cin, pointer->appointmentLocation);

	appointment_file<<endl<<pointer->appointmentID<< "; " <<pointer->appointmentWith<<"; "<<pointer->appointmentDate.Day<<"; "<<pointer->appointmentDate.Month<<"; "<<pointer->appointmentDate.Year<<"; "  << pointer->appointmentStartTime<< "; "<< pointer->appointmentDuration<< "; "<<pointer->appointmentLocation<< ";";

	appointment_file.close();

	cout<< "\nWould you like to enter another new appointment?\n";
	cout<< "Enter 1 for yes and 0 for no: ";
	cin>>repeat;
	//getline (cin, line);
	cout<<endl;

	return repeat;
};

void show_all()
{
	fstream appointment_file;
	appointment_file.open("Appointments.dat");

	string line;

	//		int appointmentID;
	//		string appointmentWith;

			string id, appwith, day, month, year, starttime, duration, location;
		
			cout << setw(8) << "ID" << setw(18) << "With" << setw(5) << "Day" << setw(6) << "Month" << setw(5) << "Year" << setw(6) << "Time" << setw(10) << "Duration" << setw(15) << "Location" << endl << endl;
			
			while (getline(appointment_file, line))
			{

				istringstream isstream (line);
 
				getline(isstream,id,';');
				getline(isstream,appwith,';');
				getline(isstream,day,';');
				getline(isstream,month,';');
				getline(isstream,year,';');
				getline(isstream,starttime,';');
				getline(isstream,duration,';');
				getline(isstream,location,';');

				cout << setw(8) << id << setw(18) << appwith << setw(5) << day << setw(6) << month << setw(5) << year << setw(6) << starttime << setw(10) << duration << setw(15) << location << endl;
			}
};

int check_id(int ID)
{
	fstream appointment_file;
	appointment_file.open("Appointments.dat");
	string line;
	int already_used=0;
	while(getline(appointment_file, line))
	{
		int appointmentID;
		appointment_file>>appointmentID;
		if (appointmentID==ID)
		{
			cout<< "\nAn appointment with that ID already exists. Please try again.\n\n";
			already_used=1;
			return already_used;
		}
	}
	return already_used;
};


Thank you for your help!



Last edited on
Put code tags around your code so it becomes easier to read, and meanwhile check whether you actually close files you previously opened again.
Sorry about that. Code tags have been added.

So if I close the files then the edit_line function will work?
Last edited on
Depends on what you are trying to achieve with edit_line. It will do something, but I do question that it will do what you want it to.
I'm certain it won't. My question is, how do I edit something in the "Appointments.dat" file? If I wanted to replace who the appointment was with, how do I do that?

Thank you.
Topic archived. No new replies allowed.