Trouble reading text file data

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
int readEmp(EMPLOYEE e[], int size)
{
	ifstream inFile;
	int numEmp = 0;
	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.ignore(255, '\n');
			inFile.getline(e[i].name, 50, ',');
			inFile.getline(e[i].id, 20, ',');
			inFile.getline(e[i].ic, 20, ',');
			inFile.getline(e[i].birthdate, 20, ',');
			inFile.getline(e[i].address, 50, ',');
			inFile.getline(e[i].phonenum, 50, ',');	
			
			numEmp++;
		}
	}
	inFile.close();
	return numEmp;
}


This is the text file im trying to read
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, 4896489, 255019971298, 27/11/1963, Tanjung Rambutan, 0123456789
Cheah Choooon Kit, 1919191, 952417881387, 08/01/1964, Penang Butterfly Farm, 013456789
Ong Jing Wei, 1997821, 781131524892, 31/11/1978, Bintulu, 013456789
Song Chi Ken, 6942069, 990511696969, 11/5/1999, Tasik Selatan, 013456789
Yua Mikami, 6969696, 930816691111, 16/8/1993, Tokyo, 013356789
Yokohama Jesus, 8899884, 880828888888, 28/8/1988, Johor Bahru, 016456789
Jack Ma, 5961495, 824426655644, 06/01/1960, Kota Kinabalu, 013456389
Ho Lee Sheet, 7855261, 630517424304, 17/05/1963, Georgetown, 013476789
Ho Lee Fuk, 4523676, 394511917501, 25/03/1962, Seremban, 013456789
Poh Kee Mak, 4863218, 55874672875, 25/05/1966, Butterworth, 013256789
Alexander the Great, 7532554, 655370358935, 21/02/1967, Sungai Buloh, 011456789
Tan Kwok Jiao, 3234590, 633884526568, 23/10/1968, Puchong, 013456189
Chin Heong Gai, 1958069, 352343813189, 03/05/1971, Sepang, 013456889
Chee Cheong Fan, 4103336, 269034563893, 01/09/1983, Kota Damansara, 010456789
Yong Tau Foo, 9909556, 582017636284, 26/06/1984, Sungai Long, 013456786
Char Goy Teau, 6770131, 379736795009, 12/11/1985, Ampang, 013456389
Ali Mohammand bin Salleh, 6891432, 417777579596, 11/08/1968, Putrajaya, 013356789
Mithran Ramesh, 2857885, 115994500762, 14/09/1970, Klang, 013453389
Sree Amathra, 4291852, 920568603674, 30/08/1971, Bukit Jalil, 0144456789
Koay Hong Zhao, 2869788, 734121544852, 08/05/1975, Setapak, 013756789
Noor Akmanizam Amin, 1218024, 372762041916, 15/11/1975, Segambut, 0133456789
Hairul Hasmoni, 3267324, 777250289768, 26/05/1982, Kluang, 0134567849
Lim Yong Xing, 2287588, 411328018912, 07/12/1984, Segamat, 0134567859
Jane Lim Huey Shan, 7385945, 681634821779, 15/09/1987, Batu Pahat, 0163456789
Muthu Kurusami, 2021110, 614657644268, 19/06/1989, Klang, 0134567889
Loh Han Som, 3409876, 088152074511, 21/06/1991, Pudu, 0134567999
Mai Ham Sap, 8564975, 156491217290, 10/01/1993, Ipoh, 0134567822
Chin Han Seng, 0624792, 099183765412, 07/02/1983, Taiping, 013466789
Chong Kuk Song, 3418324, 620007999271, 15/09/1983, Bagan Ajam, 0111156789
Xi Jin Ping, 7452625, 743496700582, 04/04/1987, Kota Malaka, 013111789


Im trying to store the data in the text file into the structure variable and then return the total number of the employees in the text file, but when i tried to display the data it does not match the one im trying to do. Is there any problem with the logic of my code ?
Try this. The slight complication with reading the file is that there are space(s) following the , which have to be discarded.

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
#include <iostream>
#include <fstream>
#include <iomanip>

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

size_t readEmp(EMPLOYEE e[], size_t size)
{
	std::ifstream inFile("employee.txt");

	if (!inFile)
		return (std::cout << "Error opening file!\nCheck the name of the file !\n"), 0;

	size_t numEmp {};

	for (; numEmp < size &&
		inFile.getline(e[numEmp].name, 50, ',') && inFile >> std::ws &&
		inFile.getline(e[numEmp].id, 20, ',') && inFile >> std::ws &&
		inFile.getline(e[numEmp].ic, 20, ',') && inFile >> std::ws &&
		inFile.getline(e[numEmp].birthdate, 20, ',') && inFile >> std::ws &&
		inFile.getline(e[numEmp].address, 50, ',') && inFile >> std::ws &&
		inFile.getline(e[numEmp].phonenum, 50);

		numEmp++
	);

	return numEmp;
}

void display(const EMPLOYEE e[], size_t size)
{
	for (size_t i = 0; i < size; ++i) {
		const auto& emp {e[i]};

		std::cout << emp.name << "  " << emp.id << "  " << emp.ic << "  " << emp.birthdate << "  " << emp.address << "  " << emp.phonenum << '\n';
	}
}

int main()
{
	const size_t MaxEmp {40};

	EMPLOYEE emps[MaxEmp];

	if (const auto read {readEmp(emps, MaxEmp)}; read) {
		display(emps, read);
		std::cout << read << " records were read\n";
	}
}



Ong Jun Ren  4896489  255019971298  27/11/1963  Tanjung Rambutan  0123456789
Cheah Choooon Kit  1919191  952417881387  08/01/1964  Penang Butterfly Farm  013456789
Ong Jing Wei  1997821  781131524892  31/11/1978  Bintulu  013456789
Song Chi Ken  6942069  990511696969  11/5/1999  Tasik Selatan  013456789
Yua Mikami  6969696  930816691111  16/8/1993  Tokyo  013356789
Yokohama Jesus  8899884  880828888888  28/8/1988  Johor Bahru  016456789
Jack Ma  5961495  824426655644  06/01/1960  Kota Kinabalu  013456389
Ho Lee Sheet  7855261  630517424304  17/05/1963  Georgetown  013476789
Ho Lee Fuk  4523676  394511917501  25/03/1962  Seremban  013456789
Poh Kee Mak  4863218  55874672875  25/05/1966  Butterworth  013256789
Alexander the Great  7532554  655370358935  21/02/1967  Sungai Buloh  011456789
Tan Kwok Jiao  3234590  633884526568  23/10/1968  Puchong  013456189
Chin Heong Gai  1958069  352343813189  03/05/1971  Sepang  013456889
Chee Cheong Fan  4103336  269034563893  01/09/1983  Kota Damansara  010456789
Yong Tau Foo  9909556  582017636284  26/06/1984  Sungai Long  013456786
Char Goy Teau  6770131  379736795009  12/11/1985  Ampang  013456389
Ali Mohammand bin Salleh  6891432  417777579596  11/08/1968  Putrajaya  013356789
Mithran Ramesh  2857885  115994500762  14/09/1970  Klang  013453389
Sree Amathra  4291852  920568603674  30/08/1971  Bukit Jalil  0144456789
Koay Hong Zhao  2869788  734121544852  08/05/1975  Setapak  013756789
Noor Akmanizam Amin  1218024  372762041916  15/11/1975  Segambut  0133456789
Hairul Hasmoni  3267324  777250289768  26/05/1982  Kluang  0134567849
Lim Yong Xing  2287588  411328018912  07/12/1984  Segamat  0134567859
Jane Lim Huey Shan  7385945  681634821779  15/09/1987  Batu Pahat  0163456789
Muthu Kurusami  2021110  614657644268  19/06/1989  Klang  0134567889
Loh Han Som  3409876  088152074511  21/06/1991  Pudu  0134567999
Mai Ham Sap  8564975  156491217290  10/01/1993  Ipoh  0134567822
Chin Han Seng  0624792  099183765412  07/02/1983  Taiping  013466789
Chong Kuk Song  3418324  620007999271  15/09/1983  Bagan Ajam  0111156789
Xi Jin Ping  7452625  743496700582  04/04/1987  Kota Malaka  013111789
30 records were read

Hello NiceS,

Working of your original code I came up with this to give you something to think about.
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
//#include <cctype>

#include <fstream>

constexpr unsigned MAXSIZE{ 40 };
constexpr unsigned MAXNAME{ 51 };  // <--- Should be 1 extra for the (\0) to mark the end of the string.
//constexpr unsigned MAX  // <--- Need something similar for the smaller arrays. Names are up to you.

struct Employee
{
    char name[MAXNAME]{};  // <--- These would work better as a "std::string".
    char id[20]{};
    char ic[20]{};
    char birthdate[20]{};
    char address[MAXNAME]{};
    char phonenum[MAXNAME]{};
};

using Emp = Employee[MAXSIZE];

int readEmp(Emp& employee)
{
    std::ifstream inFile;
    int numEmp{};

    inFile.open("employee.txt");

    system("CLS");

    if (!inFile)
    {
        std::cout << "Error opening file!\nCheck the name of the file !\n";
        return -1;  //exit(1);  // <--- Used -1 incase "numEmps" is 1.
    }
    //else
    //{
    for (unsigned int idx = 0; idx < MAXSIZE && inFile.getline(employee[idx].name, MAXNAME, ','); idx++, numEmp++)
    {
        //inFile.ignore(255, '\n');  // <--- The file pointer is at the beginning. There is nothing to ignore.
        //inFile.getline(employee[idx].name, 50, ',');  // <--- Moved to for loop.

        inFile.get();  // <--- Eats the leading white space.
        inFile.getline(employee[idx].id, 20, ',');  // <--- These magic numbers should be changed to a constant variable.
        inFile.get();
        inFile.getline(employee[idx].ic, 20, ',');
        inFile.get();
        inFile.getline(employee[idx].birthdate, 20, ',');
        inFile.get();
        inFile.getline(employee[idx].address, 50, ',');
        inFile.get();
        inFile.getline(employee[idx].phonenum, 50/*, ','*/);  // <--- There is no comma after phone number.

        //numEmp++;
    }
    //}

    //inFile.close();  // <--- Not required as the dtor will close the file when the function looses scope.
    return numEmp;
}

void Display(const Emp& employee, int numEmps)
{
    std::cout <<
        "\n"
        "    Name                     ID         IC         Birth Date           Address             Phone\n";
    std::cout << std::string(100, '-') << '\n';

    for (int idx = 0; idx < numEmps; idx++)
    {
        const Employee emp{ employee[idx] };

        std::cout << std::left <<
            std::setw(27) << emp.name <<
            std::setw(9) << emp.id <<
            std::setw(15) << emp.ic <<
            std::setw(15) << emp.birthdate <<
            std::setw(24) << emp.address <<
            std::setw(12) << emp.phonenum <<
            std::right << std::setw(2) << idx + 1 << '\n';  // <--- Up to the ('\n) is optional or not needed. Just to show how many lines printed.
    }
}

int main()
{
    Employee employee[MAXSIZE];

    int response{}, numEmps{};
    
    if ((response = readEmp(employee)) == -1)
        return 1;
    
    numEmps = response;

    //std::cout << "\n number of employees is: " << numEmps << "\n\n"; // <--- Used for testing. Comment or remove when finished.

    Display(employee, numEmps);


	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}


Something different to look over.

Andy
Use >> ws instead of .get() as .get() assumes only 1 white-space whereas >> ws will consume all white-space.

Topic archived. No new replies allowed.