update Records in a text file

How to update records in a text file?
Last edited on
It depends. Can you tell us more about what you're trying to accomplish? Such as:

Are you changing 1 small thing in a big text file?

Are you changing multiple lines or just 1 line?

Are the lines different lengths, or fixed?

Then you can decide if you want to do random access and just change what you need, or perhaps rewrite the existing file into a new file with new information, etc.
Thanks for replying....:)
This is what i did 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
void FileHandling::updateEmployee(int TEMP_NUM)//update given employee
{
	int temp=0;
	char TEMP;
	Employee e;//create object of Employee class
	fstream tempfile;//create object of fstream
	tempfile.open("Employee_Details.txt",ios::in|ios::out|ios::ate|ios::app);//open the file for output
	
	string CurrentLine;
	std::string ap="#";
	stringstream s;//create a string stream object s
    s<<TEMP_NUM;//convert int to string
	ap.append(s.str());//append new string to ap
	tempfile.seekg(0,ios::beg);//get the file pointer to the begining of the file
	

	try
	{
		if(tempfile.good())//if file is available
		{
			while(!tempfile.eof())//loop until end of file 
			{
				while(getline(tempfile,CurrentLine) && !CurrentLine.empty())  
				{  
					int firstPos = CurrentLine.find(ap);//find empid in the current line  
					 if(firstPos != string::npos)
					 {
						  std::string NO,NAME,NIC,GEN,DESIGNATION,DEPARTMENT,DATEJOINED,NATIONALITY,REG,DOB,STS,PASSWORD;//declare string variables
						  CurrentLine.clear();
					  
						  			 
					  //-----------------------------------------------------------------------
						cout<<"Enter Full Name :";
						//getline(cin,NAME);
						cin>>NAME;
						cin.ignore();
						cout<<"Employee NIC Number: ";
						cin>>NIC;
						cin.ignore();
						cout<<endl;
						cout<<"Employee Gender (M/F) :";
						cin>>TEMP;
						//-----------------------------------------------------------------------
						if(TEMP=='M')
						{
							GEN="Male";
						
						}
						else if(TEMP=='F')
						{
							GEN="Female";
						
							//e.gender("Female");
						}
						else
						{
							GEN="Invalid-Entry";
							
							//e.gender("Invalid Entry");
						}
						TEMP=' ';
						cin.ignore();
						cout<<endl<<endl;
						cout<<"Employee Designation :";
						cin>>DESIGNATION;
						cin.ignore();
						cout<<endl<<endl;
						cout<<"Employee Department"<<endl;
						cout<<setw(10)<<"========================="<<endl;
						cout<<"1-Accounts\n"<<"2-Human-Resources\n"<<"3-Sales\n"<<"4-Production\n"<<"5-IT\n"<<"6-Other\n"<<endl;
						cout<<setw(10)<<"========================="<<endl<<endl;
						cout<<":=";
						cin>>temp;
						switch(temp)
						{
							case(1):
							{
								DEPARTMENT="Accounts";
								temp=0;
								break;
							}
							case(2):
							{
								DEPARTMENT="Human Resources";
								temp=0;
								break;
							}
							case(3):
							{
								DEPARTMENT="Sales";
								temp=0;
							
								break;
							}
							case(4):
							{
								DEPARTMENT="Production";
								temp=0;

								break;
							}
							case(5):
							{
								DEPARTMENT="IT";
								temp=0;

								break;
							}
							case(6):
							{
								DEPARTMENT="Other";
								temp=0;

								break;
							}
							default:
							{
								cout<<"Department Is Not Valid"<<endl;
								DEPARTMENT="Invalid-Entry";
								temp=0;

								break;
							}
						}
						cin.ignore();
						cout<<"Employee Date Joined (DD-MM-YYYY):";
						cin>>DATEJOINED;
						cin.ignore();
						cout<<endl;
						cout<<"Employee Nationality :";
						cin>>NATIONALITY;
						cin.ignore();
						cout<<endl;
						temp=0;
						cout<<"Employee Religion (1-Buddhist,2-Catholic,3-Hindu,4-Muslim,5-Other) :";
						cin>>temp;
						switch(temp)
						{
						case(1):
							{
								REG="Buddhist";
								temp=0;
								break;
								
							}
						case(2):
							{
								REG="Catholic";
								temp=0;
								break;
						
							}
						case(3):
							{
								REG="Hindu";
								temp=0;
								break;
						
							}
						case(4):
							{
								REG="Muslim";
								temp=0;
								break;
						
							}
						case(5):
							{
								REG="Other";
								temp=0;
								break;
							}
						default:
							{
								REG="Invalid-Entry";
								temp=0;
								break;
							}
						}
						cout<<endl;
						cout<<"Employee Date Of Birth (DD-MM-YYYY):";
						cin>>DOB;
						cin.ignore();
						cout<<endl;
						temp=0;
						cout<<"Employee Marital Status (1-Married , 2-Single):";
						cin>>temp;
						if(temp=1)
							STS="Married";
						else
							STS="Single";

						cin.ignore();
						cout<<endl;
						cout<<"Employee Default Password: ";
						cin>>PASSWORD;
						cin.ignore();
						/*CurrentLine.append(ap);*/
						CurrentLine.push_back(tempfile.get());

					    tempfile.clear();//clear the temp file object
					 }

				/*	 else
					 {

					 }*/
				 }  
			}
			tempfile.close(); //close file 
		}
							
	}

	catch(...)
	{
		cout<<"Exception occurred-Record Is Not Available";
	}
}


this is my logic. what i want to do is to
1. Ask from the user what is the employee id he wants to update
2.search that employee id in the file
3. if it is available
3.1 take the data in to current line
3.2 break the current line in to strings
3.3 take the user input in to those strings
3.4 make the current line blank
3.5 append those strings to current line
3.6 Push back the current line in to the file
4.if not just give a msg to the user :)

if there is any efficient method to update values in text file i request them to comment. I searched on da web. I couldn't find a proper answer for this updating thing.
Last edited on
One of the problems with editing an existing text file is that in order to add information that goes beyond the current length of the line, the rest of the file will need to be moved over. Because of this problem, many programs either:

1) use fixed-width lines, or
2) rewrite the entire file each time there is a change.

The pseudo logic for option 1 is:

Open the file in a "random access" way
Search for the needed line
Replace the line with the new information
Close the file.

The pseudo logic for option 2 is:

Start reading the input file
In a loop, read the next record until there are no more records.
If we found the record we want:
Write the new data to the output file
else
Write the old data to the output file
end loop
Close the input and output files, erase the input file and rename the output file.

While there are more steps involved for option 2, IMHO it is the easier option. Performance-wise, I'd think that option 1 is better. But I'd have to say it would depend on your implementation.
Yes ..performance wise option 1 is vgood. (i'm working on it :))

back to option 2
.........................

it is very inefficient to rewrite the file every time when you update a record. Is there any other ways to do this?
Last edited on
Another option is to use more of a "database" type model. You can use any of the readily available libraries, or roll your own.

With a database model, you have rows and columns like a spreadsheet. In addition, you can have indexes that allow you to do quick searches on your data.
Thanks a lot :) :):)
Topic archived. No new replies allowed.