File getting deleted instead of appending

Im trying to create a function that checkout a tool for a user when called, currently my entire checkout file is getting deleted and not getting appended.
The data required is being passed to this function through function call and the data have all been stored successfully
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
  typedef struct
{
	char code[5];
	char name[30];
	int quantity;
}TOOL;

typedef struct
{
	char name[50];
	char id[20];
	char ic[20];
	char department[50];
	char phonenum[20];
}EMPLOYEE;

typedef struct
{
	char empName[30];
	char empID[20];
	char toolName[30];
	char toolID[5];
	char date[15];
}CO;

void checkoutTools(CO[], EMPLOYEE[], TOOL[], int, int, int);

void checkoutTools(CO c[], EMPLOYEE e[], TOOL t[], int cNum, int eNum, int tNum)
{
	char tCode[5];
	char eCode[10];
	char date[15];
	int qty, ret;
	int i, j = 1, found = 0;
	char repeat;
	int eLoc = -1, tLoc;
	ofstream outFile, oFile;
	outFile.open("tools.txt");
	oFile.open("checkout.txt", ios::app);

	while (j) //continue to update records until j is changed
	{
		system("CLS");
		cout << fixed << left;
		cout << setw(10) << "ID";
		cout << setw(30) << "Name";
		cout << setw(10) << "Quantity";
		cout << endl;

		for (int x = 0; x < tNum; x++)
		{
			cout << setw(10) << t[x].code;
			cout << setw(30) << t[x].name;
			cout << setw(10) << t[x].quantity;
			cout << endl;
		}
		cout << endl << "This are the tools we have, what do you need ?\n";
		cout << "Enter tool ID you want: ";
		cin.ignore();
		cin.getline(tCode, 10);
		cout << "\nEnter your employee ID: ";
		cin.getline(eCode, 10);
		//To check employee exist
		for (int y = 0; y < eNum; y++)
		{
			if (_strcmpi(eCode, e[y].id) == 0)
				eLoc = y;
		}

		if (eLoc != -1)
		{
			for (i = 0; i < tNum; i++)
			{
				if ((_strcmpi(tCode, t[i].code) == 0)) //to find the records from text file
				{
					//Display current data
					tLoc = i;
					cout << fixed << left;
					cout << setw(10) << "Code";
					cout << setw(30) << "Name";
					cout << setw(10) << "Quantity";
					cout << endl;
					cout << setw(10) << t[i].code;
					cout << setw(30) << t[i].name;
					cout << setw(10) << t[i].quantity;
					cout << endl;
					do
					{
						cout << "How many do you need ? ";
						cin >> qty;
						if (t[i].quantity < qty)
						{
							cout << "\nInsufficienct amount !\n";
						}
					} while (t[i].quantity < qty);
					t[i].quantity = t[i].quantity - qty;
					cout << "Enter Checkout Date: ";
					cin.ignore();
					cin.getline(date, 15);
					cout << "\nCheckout Successful" << endl;
					found++;
				}
			}
			if (found == 0)
				cout << "\nRecords not found!" << endl;
			cout << "Enter y to checkout another tools" << endl;
			cin.clear();
			cin >> repeat;
			repeat = tolower(repeat);
			if (repeat != 'y')
				j = 0; //End the loop
		}
	}
	for (int k = 0; k < tNum; k++)
	{
		outFile << t[k].code << "," << t[k].name << "," << t[k].quantity << endl;
	}
	outFile.close();
	oFile << e[eLoc].name << "," << e[eLoc].id << "," << t[tLoc].name << "," << t[tLoc].code << "," << date << endl;
	oFile.close();
}


Checkout txt file
1
2
3
4
5
6
7
8
9
10
Ong Jun Ren,1500000,ladder,101,2/2/2021
Cheah Chooon Kit,1500001,drill,104,25/2/2021
Chee Cheong Fan,1500013,ladder,101,25/2/2021
Ali Mohammand bin Salleh,1500016,sewing machine,102,25/2/2021
Jack Ma,1500006,tape measure,105,26/2/2021
Tan Kwok Jiao,1500011,trolley,103,26/2/2021
Chin Heong Gai,1500012,ladder,101,26/2/2021
Yokohama Jesus,1500005,step ladder,106,27/2/2021
Sree Amathra,1500018,hammer,107,27/2/2021
Char Goy Teau,1500015,flat-head screwdriver,108,1/3/2021

employee text file
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,Renovation,0123456789
Cheah Chooon Kit,1500001,952417881387,Mechanical,013456789
Ong Jing Wei,1500002,781131524892,Electrical,013456789
Char Koal,1500003,990511696969,General Maintenance,013456789
Yua Mikami,1500004,930816691111,Electrical,013356789
Yokohama Jesus,1500005,880828888888,Electrical,016456789
Jack Ma,1500006,824426655644,Mechanical,013456389
Ho La,1500007,630517424304,Renovation,013476789
Ho Lee,1500008,394511917501,Mechanical,013456789
Alice Tan,1500009,55874672875,Environmental,013256789
Alexander the Great,1500010,655370358935,Environmental,011456789
Tan Kwok Jiao,1500011,633884526568,Renovation,013456189
Chin Heong Gai,1500012,352343813189,Mechanical,013456889
Chee Cheong Fan,1500013,269034563893,Mechanical,010456789
Yong Tau Foo,1500014,582017636284,General Maintenance,013456786
Char Goy Teau,1500015,379736795009,General Maintenance,013456389
Ali Mohammand bin Salleh,1500016,417777579596,General Maintenance,013356789
Mithran Ramesh,1500017,115994500762,Renovation,013453389
Sree Amathra,1500018,920568603674,Electrical,0144456789
Koay Hong Zhao,1500019,734121544852,Electrical,013756789
Noor Akmanizam Amin,1500020,372762041916,Mechanical,0133456789
Hairul Hasmoni,1500021,777250289768,Mechanical,0134567849
Lim Yong Xing,1500022,411328018912,Environmental,0134567859
Jane Lim Huey Shan,1500023,681634821779,Electrical,0163456789
Muthu Kurusami,1500024,614657644268,Renovation,0134567889
Loh Han Som,1500025,088152074511,Electrical,0134567999
Mai Ham Sap,1500026,156491217290,Mechanical,0134567822
Chin Han Seng,1500027,099183765412,General Maintenance,013466789
Chong Kuk Song,1500028,620007999271,Renovation,0111156789
Xi Jin Ping,1500029,743496700582,Environmental,013111789

Tool txt file
1
2
3
4
5
6
7
8
9
10
101,ladder,20
102,sawing machine,15
103,trolley,10
104,drill,30
105,tape measure,50
106,step ladder,20
107,hammers,50
108,flat-head screwdriver,50
109,philips screwdriver,50
110,spanner,60
1
2
3
ofstream outFile, oFile;
outFile.open("tools.txt");
oFile.open("checkout.txt", ios::app);


All the data in tools.txt will be first deleted before new data is added
Data will be appended to checkout.txt.
How do this happend exactly since i open the text file seperately using different variable name ? So to tix this i can only open one file at a time and when im done with one file close it and open another one ?
No. How you open a file has no bearing on how you open another file. All the data in tools.txt will be removed when the file in opened. checkout.txt will have data appended to the end of existing data.

If you wish to also append to the end of tools.txt, then also open that file as ios::app.

Note that having one file variable called oFile and another called outFile isn't very good naming! It would be better to have some sense in the variable name as to which file it refers.

Note that as you haven't posted all the code, we don't know what happens to these files in other parts of the program. Are you opening checkout.txt elsewhere in the program?
Im unable to post my full code as it is very long and is part of an assignment i have and thus im only showing the problem im having. The checkout.txt is open in another function however it is only to display the info in the file and was close afterwards. Now i did open the tools file because i needed to update the tools quantity after being checkedout and therefore i need to rewrite the file but i need to append specific info as above to the checkout file thus i open it in append mode
What i dont get is that why is my tools file is getting deeted because of this ? Since i see no relationship between the two files
 
outFile.open("tools.txt");


This will delete the tools.txt file data. Opening a file as ofstream deletes the existing data by default.

Last edited on
Topic archived. No new replies allowed.