Writing to text file

Im trying to add in another extra row of data to the checkout txt file whenever a tool is checked out. The checkout file consist data in the form of employee name, id, tool name, code and date input by the user. The employee, tools and checkout data is stored in a structure respectively. Is there any way of writing a program that do it automatically ?


This is employee txt
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,1500005,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

This is tool txt
1
2
3
4
5
6
7
8
9
10
101,ladder,20
102,sawing machine,15
103,trolley,10
104,drill,30
105,toolbox,50
106,step ladder,20
107,hammers,50
108,flat-head screwdriver,50
109,philips screwdriver,50
110,spanner,60

this is checkout txt
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,toolbox,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
This is a little sample program I wrote while learning to write to files with fstream, play with this a bit and you should be able to do what you wish.
I'm a novice developer so anything another user posts is likely a better idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
WritingNewLine.cpp
Open file and append a new line
This sample actually writes 10 new lines and exits
*/
#include <fstream>
using namespace std;
ofstream MyFile;
int var0;

int main() {
	
while(var0 < 10){
	var0++;
	MyFile.open ("sample.txt", ios_base::app); // append mode, add new line to existing file
	MyFile << "New Line number " << var0 << endl;
	MyFile.close();
}
}


Output written to sample.txt:
1
2
3
4
5
6
7
8
9
10
New Line number 1
New Line number 2
New Line number 3
New Line number 4
New Line number 5
New Line number 6
New Line number 7
New Line number 8
New Line number 9
New Line number 10


The format of the employee file is odd, if the employee number was first (like in the tool file) it wouldn't be difficult to grab that line from the employee number (or partial employee number since 15000 could simply be formatted in).
Last edited on
Yes i did tried to write it in the program however the output it gave me when i tried to add in a new line of data was all written in random symbols and such
Can you post a sample program?
Im trying to add in another extra row of data to the checkout txt file


At the end, or somewhere in the file? If at the end, just open the file for append and then write. This will add the written contents to the end of the file.

If somewhere in the file, this is more complicated. The easiest way is to read the whole file into a container (std::vector, std::list?), modify the container as required and write the whole container back to the file.

Post your current code for doing this so that we can advise further.
Last edited on
Topic archived. No new replies allowed.