Trouble reading and displaying text file

Sorry for the long code but im trying to develop a function that is call by the main function to read multiple data from different text file and store it in the variable. But im having trouble to read the last text file the one included below. The text file for the other structure was fine but the problem only aris on the last checkout 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
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cctype>
#include<cstring>
#include<cstdlib>

using namespace std;

 typedef struct
{
	char code[5];
	char name[30];
	int quantity;
}MATERIAL;

typedef struct
{
	char code[5];
	char name[30];
	int quantity;
}TOOL;

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

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

void readData(MATERIAL [], TOOL [], EMPLOYEE [], CO [], int , int* , int* , int* , int*);

int main(void)
{
	int matNum = 0, toolNum = 0, empNum = 0, coNum = 0;
	TOOL tools[50];
	MATERIAL mats[50];
	EMPLOYEE emps[50];
	CO cot[50];
	readData(mats, tools, emps, cot, 50, &matNum, &toolNum, &empNum, &coNum);
	for (int i = 0; i < coNum; i++)
	{
		cout << cot[i].empName << "," << cot[i].empID << "," << cot[i].toolName 
			<< "," << cot[i].toolID << "," << cot[i].date << endl;
	}
	system("PAUSE");
}

void readData(MATERIAL m[], TOOL t[], EMPLOYEE e[],CO cot[], int size, int* mNum, int* tNum, int* eNum, int *coNum)
{
	ifstream inFile;
	ifstream in_file;
	*mNum = 0, *tNum = 0, *eNum = 0;
	inFile.open("materials.txt");
	system("CLS");
	//Read Materials Data
	if (!inFile)
	{
		cout << "Error opening file !\nCheck the name of the file !\n";
		exit(1);
	}
	else
	{
		for (int i = 0; !inFile.eof(); i++)
		{

			inFile.getline(m[i].code, 5, ',');
			inFile.getline(m[i].name, 30, ',');
			inFile >> m[i].quantity;
			inFile.ignore(255, '\n');
			(*mNum)++;
		}
		inFile.close();
	}

	inFile.open("tools.txt");
	system("CLS");
	if (!inFile)
	{
		cout << "Error opening file!\nCheck the name of the file !\n";
		exit(1);
	}
	else
	{
		for (int i = 0; !inFile.eof(); i++)
		{
			inFile.getline(t[i].code, 5, ',');
			inFile.getline(t[i].name, 30, ',');
			inFile >> t[i].quantity;
			inFile.ignore(255, '\n');
			(*tNum)++;
		}
	}
	inFile.close();

	inFile.open("employee.txt");
	system("CLS");
	if (!inFile)
	{
		cout << "Error opening file!\nCheck the name of the file !\n";
		exit(1);
	}
	else
	{
		for (int i = 0; !inFile.eof(); i++)
		{
			inFile.getline(e[i].name, 30, ',');
			inFile.getline(e[i].id, 20, ',');
			inFile.getline(e[i].ic, 20, ',');
			inFile.getline(e[i].birthdate, 20, ',');
			inFile.getline(e[i].address, 30, ',');
			inFile.getline(e[i].phonenum, 30);
			(*eNum)++;
		}
	}
	inFile.close();
	
	in_file.open("checkout.txt");
	system("CLS");
	if (!in_file)
	{
		cout << "Error opening file!\nCheck the name of the file !\n";
		exit(1);
	}
	else
	{
		for (int i = 0; !in_file.eof(); i++)
		{
			
			in_file.getline(cot[i].empName, 30, ',');
			in_file.getline(cot[i].empID, 20, ',');
			in_file.getline(cot[i].toolName, 30, ',');
			in_file.getline(cot[i].toolID, 5, ',');
			in_file.getline(cot[i].date, 15);
			
			(*coNum)++;
		}
	}
	in_file.close();
	
}

The text file im trying to read and display
1
2
3
4
5
6
7
8
9
10
Ong Jun Ren,4896489,ladder,101,2/2/2021
Cheah Choooon Kit,1919191,drill,104,25/2/2021
Chee Cheong Fan,4103336,ladder,101,25/2/2021
Ali Mohammand bin Salleh,6891432,102,sewing machine,25/2/2021
Jack Ma,5961495,toolbox,105,26/2/2021
Tan Kwok Jiao,3234590,trolley,103,26/2/2021
Chin Heong Gai,1958069,ladder,101,26/2/2021
Yokohama Jesus,8899884,step ladder,106,27/2/2021
Sree Amathra,4291852,hammer,107,27/2/2021
Char Goy Teau,6770131,108,flat-head screwdriver
Last edited on
Hello NiceS,

It is best to provide a complete program that can compile and run. Here I have to guess at the header files and the ability to run the program here is un available because of the missing header files.

Andy
Added in the header sorry for the inconvenience
The code for reading from files is not correct. You need to check for file error (and eof) after the file read operation, not before as here.

As an example:

1
2
3
4
5
6
7
8
9
for (int i = 0; i < size && in_file.getline(cot[i].empName, 30, ','); ++i) {
		in_file.getline(cot[i].empID, 20, ',');
		in_file.getline(cot[i].toolName, 30, ',');
		in_file.getline(cot[i].toolID, 5, ',');
		in_file.getline(cot[i].date, 15);

		if (in_file)
			(*coNum)++;
	}


Also, in the text file data above, the last line is incomplete. Also, the order of the data for eh sewing machine is not right.

Use of typdef like this is very c-ish and not needed with C++.
Last edited on
For just reading the checkout.txt file as an example:

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
#include <iostream>
#include <fstream>
using namespace std;

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

void readData(CO[], size_t, size_t*);

int main(void)
{
	constexpr size_t maxsze {50};

	size_t coNum {};
	CO cot[maxsze] {};

	readData(cot, maxsze, &coNum);

	for (size_t i = 0; i < coNum; ++i) {
		cout << cot[i].empName << "," << cot[i].empID << "," << cot[i].toolName
			<< "," << cot[i].toolID << "," << cot[i].date << '\n';
	}
}

void readData(CO cot[], size_t size, size_t* coNum)
{
	ifstream in_file ("checkout.txt");

	if (!in_file) {
		cout << "Error opening file!\nCheck the name of the file !\n";
		exit(1);
	}

	for (size_t i = 0; i < size && in_file.getline(cot[i].empName, 30, ','); ++i) {
		in_file.getline(cot[i].empID, 20, ',');
		in_file.getline(cot[i].toolName, 30, ',');
		in_file.getline(cot[i].toolID, 5, ',');
		in_file.getline(cot[i].date, 15);

		if (in_file)
			(*coNum)++;
	}
}


which for the file data


Ong Jun Ren,4896489,ladder,101,2/2/2021
Cheah Choooon Kit,1919191,drill,104,25/2/2021
Chee Cheong Fan,4103336,ladder,101,25/2/2021
Ali Mohammand bin Salleh,6891432,sewing machine,102,25/2/2021
Jack Ma,5961495,toolbox,105,26/2/2021
Tan Kwok Jiao,3234590,trolley,103,26/2/2021
Chin Heong Gai,1958069,ladder,101,26/2/2021
Yokohama Jesus,8899884,step ladder,106,27/2/2021
Sree Amathra,4291852,hammer,107,27/2/2021


displays


Ong Jun Ren,4896489,ladder,101,2/2/2021
Cheah Choooon Kit,1919191,drill,104,25/2/2021
Chee Cheong Fan,4103336,ladder,101,25/2/2021
Ali Mohammand bin Salleh,6891432,sewing machine,102,25/2/2021
Jack Ma,5961495,toolbox,105,26/2/2021
Tan Kwok Jiao,3234590,trolley,103,26/2/2021
Chin Heong Gai,1958069,ladder,101,26/2/2021
Yokohama Jesus,8899884,step ladder,106,27/2/2021
Sree Amathra,4291852,hammer,107,27/2/2021

Last edited on
Hello NiceS,

The problem is not all with your code, but with the input file. Look at line 4 of the input file and see if that is in the correct order compared to the other lines.

Although I see that seeplus had decided what your program should be even though what you started with can be made to work.

Be careful using "size_t" you may be defining an "unsigned long".

Andy
@Andy - as I said, what I posted above was for just reading one file as an example. Data for the other files hasn't been posted. Once the OP has the read working for one file, then adjusting it for the others is a relatively simple task.

IMO I wouldn't have had all the file reads in one function, but a function (or even better an operator>> overload) for each file.

The type size_t is used for the type of an index and loop counting as it is unsigned and aligned. See https://en.cppreference.com/w/c/types/size_t

size_t is commonly used for array indexing and loop counting.

Topic archived. No new replies allowed.