Trouble editing text file

Im trying to delete a row of data from a text file with the input prompt from the user. Firstly user will enter the id of the staff and there would be comparison if it match it would update the variables and delete the data from text file. But my data is not deleted from the text file even though it is correct. This function is being called by another function with data pass into

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
typedef struct
{
	char name[50];
	char id[20];
	char ic[20];
	char address[50];
	char phonenum[20];
}EMPLOYEE;

void delEmps(EMPLOYEE e[], int size)
{
	char deleteID[10], confirm, repeat, ret;
	int j, i;
	ofstream outFile;
	do
	{
		cout << "Enter the emploee ID you want to delete: ";
		cin.ignore();
		cin.getline(deleteID, 10);
		for (i = 0; i < size; i++)
		{
			if (strcmp(e[i].id, deleteID) == 0)
			{
				cout << "\nRecord found, Confirmation for deletion [y for yes]: ";
				cin >> confirm;
				confirm = tolower(confirm);
				if (confirm == 'y')
				{
					cout << "\n";
					for (j = i; j < size; j++)
						e[j] = e[j + 1];
					size--; //The number of employee decrease
					cout << "\nEmployee data successfully deleted !";
				}
				else
				{
					cout << "\nRecords not found";
				}
			}
		}
		cout << "\nAny more employee data to delete ? [y/n]";
		cin >> repeat;
		repeat = tolower(repeat);
	} while (repeat == 'y');
	//UPDATE EMPLOYEE TEXT FILE
	for (int k = 0; k < size; k++)
	{
		outFile << e[k].name << "," << e[k].id << "," << e[k].ic << "," << e[k].address << "," << e[k].phonenum << endl;
	}

	outFile.close();


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
Ong Jun Ren,1500000,255019971298,Tanjung Rambutan,0123456789
Cheah Chooon Kit,1500001,952417881387,Penang Butterfly Farm,013456789
Ong Jing Wei,1500002,781131524892,Bintulu,013456789
Char Koal,1500003,990511696969,Tasik Selatan,013456789
Yua Mikami,1500004,930816691111,Tokyo,013356789
Yokohama Jesus,15000005,880828888888,Johor Bahru,016456789
Jack Ma,1500006,824426655644,Kota Kinabalu,013456389
Ho La,1500007,630517424304,Georgetown,013476789
Ho Lee,1500008,394511917501,Seremban,013456789
Alice Tan,1500009,55874672875,Butterworth,013256789
Alexander the Great,1500010,655370358935,Sungai Buloh,011456789
Tan Kwok Jiao,1500011,633884526568,Puchong,013456189
Chin Heong Gai,1500012,352343813189,Sepang,013456889
Chee Cheong Fan,1500013,269034563893,Kota Damansara,010456789
Yong Tau Foo,1500014,582017636284,Sungai Long,013456786
Char Goy Teau,1500015,379736795009,Ampang,013456389
Ali Mohammand bin Salleh,1500016,417777579596,Putrajaya,013356789
Mithran Ramesh,1500017,115994500762,Klang,013453389
Sree Amathra,1500018,920568603674,Bukit Jalil,0144456789
Koay Hong Zhao,1500019,734121544852,Setapak,013756789
Noor Akmanizam Amin,1500020,372762041916,Segambut,0133456789
Hairul Hasmoni,1500021,777250289768,Kluang,0134567849
Lim Yong Xing,1500022,411328018912,Segamat,0134567859
Jane Lim Huey Shan,1500023,681634821779,Batu Pahat,0163456789
Muthu Kurusami,1500024,614657644268,Klang,0134567889
Loh Han Som,1500025,088152074511,Pudu,0134567999
Mai Ham Sap,1500026,156491217290,Ipoh,0134567822
Chin Han Seng,1500027,099183765412,Taiping,013466789
Chong Kuk Song,1500028,620007999271,Bagan Ajam,0111156789
Xi Jin Ping,1500029,743496700582,Kota Malaka,013111789
I didn't see anywhere you open the file between line 14 and 48.

>
for (j = i; j < size; j++)
You have a j+1 subscript, so your last iteration will access out of bound of your array.
You should have
 
for (j = i; j < size-1; j++)


> void delEmps(EMPLOYEE e[], int size)
If you want the caller to see this
> size--; //The number of employee decrease
Then declare the function as
 
void delEmps(EMPLOYEE e[], int &size)

The file is read and stored in another function and the data was passed to this function and there was no problem with it.
I did realised the first problem of the array being out of bound and changed it. However the program does not run the same way as intended. Since i just want to update the text file after i deleted. Eg when i delete the first record and confirm the deletion, the text file rewrritten by line 45 to 50 is still the same and the first record is not deleted. I do not want the caller to see it unless it is called but i just want to update the text file but it is not updating
Last edited on
Thanks salem c i think i misunderstood your sentence as i have just found out i really did not open the output file to be written thank you
Topic archived. No new replies allowed.