Array of structs from a file

I have to create a program that reads in a master file of baseball stats and a file of new 2014 stats. I have to update the master file with the information from the new file. I know i have to use a structure with the individual stats and input the structure into the array for each player but i don't know how to do this. This all of the code i have so far because i don't know where to go from here.

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

struct playerInfo
{
	string playerId;
	string firstName;
	string lastName;
	string devision;
	string team;
	double plateAppearances;
	double atBats;
	double singles;
	double doubles;
	double triples;
	double homeRuns;
	double sacFlys;
	double walks;
	double hitByPitch;
	double battingAverage;
	double onBasePercentage;
	double sluggingPercentage;
};


int main()
{
	ifstream masterData;
	masterData.open("Master_Data.csv");
	ifstream newStats;
	newStats.open("2014_League_Stats.csv");

	if (masterData.is_open() == true)
	{

	}
	else
	{
		cout << "Error Reading File" << endl;
	}

	masterData.close();
	newStats.close();

	system("pause");
	return 0;
}
The (masterData.is_open() == true) doesn't need the ==, as the is_open() returns a bool value.

sounds like you need to have a while loop which reads in the new data, then you need to have a function which navigates the master file (likely with tellg(), tellp(),seekg(), seekp()) to change values as needed.

Probably easiest to read in the data field by field, do a comparison (masterfile data vs.newFile data), and put the appropriate value into the master file, then move onto the next field. Are both files formatted the same (same layout)?
Last edited on
Alright here is some updated code. The only problem is i don't know where to go from here. I know i have to update the master file with the information from the 2014 file but i have no idea how. The 2014 file uses letters to signify if a player is returning(R), not returning(D), or a new player (N). You have to update the master file to reflect these changes. If someone is returning then there stats are just updated and if someone is not returning it will delete their records and if someone is new it will add them.

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

struct playerInfo
{
	string recordType,
		   playerId,
		   firstName,
		   lastName,
		   division,
		   team;
	double plateAppearances, 
		   atBats, 
		   singles, 
		   doubles, 
		   triples, 
		   homeRun, 
		   sacFlys, 
		   walks, 
		   hitByPitch, 
		   battingAverage, 
		   onBasePercentage, 
		   sluggingPercentage;
};


int main()
{
	playerInfo playerStats[500];
	playerInfo newPlayerStats[500];
	int counter = 0;
	int otherCounter = 0;
	ifstream masterData("Master_Data.csv");
	ifstream newStats("2014_League_Stats.csv");

	if (masterData.is_open())
	{
		while (!masterData.eof())
		{
			getline(masterData, playerStats[counter].playerId, ',');
			getline(masterData, playerStats[counter].firstName, ',');
			getline(masterData, playerStats[counter].lastName, ',');
			getline(masterData, playerStats[counter].division, ',');
			getline(masterData, playerStats[counter].team, ',');
			masterData >> playerStats[counter].plateAppearances;
			masterData.ignore(',');
			masterData >> playerStats[counter].atBats;
			masterData.ignore(',');
			masterData >> playerStats[counter].singles;
			masterData.ignore(',');
			masterData >> playerStats[counter].doubles;
			masterData.ignore(',');
			masterData >> playerStats[counter].triples;
			masterData.ignore(',');
			masterData >> playerStats[counter].homeRun;
			masterData.ignore(',');
			masterData >> playerStats[counter].sacFlys;
			masterData.ignore(',');
			masterData >> playerStats[counter].walks;
			masterData.ignore(',');
			masterData >> playerStats[counter].hitByPitch;
			masterData.ignore(',');
			masterData >> playerStats[counter].battingAverage;
			masterData.ignore(',');
			masterData >> playerStats[counter].onBasePercentage;
			masterData.ignore(',');
			masterData >> playerStats[counter].sluggingPercentage;
			masterData.ignore(',');
		}
	}
	else
	{
		cout << "Error Reading File" << endl;
	}

	if (newStats.is_open())
	{
		while (!newStats.eof())
		{
			getline(newStats, newPlayerStats[otherCounter].recordType, ',');
			getline(newStats, newPlayerStats[otherCounter].playerId, ',');
			getline(newStats, newPlayerStats[otherCounter].firstName, ',');
			getline(newStats, newPlayerStats[otherCounter].lastName, ',');
			getline(newStats, newPlayerStats[otherCounter].division, ',');
			getline(newStats, newPlayerStats[otherCounter].team, ',');
			newStats >> newPlayerStats[otherCounter].plateAppearances;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].atBats;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].singles;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].doubles;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].triples;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].homeRun;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].sacFlys;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].walks;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].hitByPitch;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].battingAverage;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].onBasePercentage;
			newStats.ignore(',');
			newStats >> newPlayerStats[otherCounter].sluggingPercentage;
			newStats.ignore(',');
		}
	}
	else
	{
		cout << "Error Reading File" << endl;
	}

	masterData.close();
	newStats.close();

	system("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.