Code:
#include <stdio.h>
#include <stdlib.h>
int ind, gamesPlayed, goals, assists, points, plusMinus, penaltyMins, ppGoals, shGoals, gwGoals, otGoals, shots;
float shotPercent, shiftsPerGame, faceOffWinPercent;
char player[50];
char team[4];
char pos[2];
char timeOnIce[10];
int strPlayer = 0;
int strTimeOnIce = 0;
int getNumPlayers(char *fileName)
{
int numPlayers = 0;
FILE *fp;
fp = fopen(fileName, "r");
while (!feof(fp))
{
fscanf(fp, "%d %s %s %c %d %d %d %d %d %d %d %d %d %d %d %f %s %f %f", &ind, player, team, &pos, &gamesPlayed, &goals, &assists, &points, &plusMinus, &penaltyMins, &ppGoals, &shGoals, &gwGoals, &otGoals, &shots, &shotPercent, timeOnIce, &shiftsPerGame, &faceOffWinPercent);
numPlayers++;
}
printf("Number of players: %d\n", numPlayers+1); //add 1 to account for starting point of 0
fclose(fp);
return numPlayers;
}
struct stats
{
int ind;
char player[25];
char team[4];
char pos[2];
int gamesPlayed;
int goals;
int assists;
int points;
int plusMinus;
int penaltyMins;
int ppGoals;
int shGoals;
int gwGoals;
int otGoals;
int shots;
float shotPercent;
char timeOnIce[6];
float shiftsPerGame;
float faceOffWinPercent;
};
int getLength(int numPlayers, char *fileName)
{
FILE *fp;
fp = fopen(fileName, "r");
while (!feof(fp))
{
fscanf(fp, "%d %s %s %c %d %d %d %d %d %d %d %d %d %d %d %f %s %f %f", &ind, player, team, &pos, &gamesPlayed, &goals, &assists, &points, &plusMinus, &penaltyMins, &ppGoals, &shGoals, &gwGoals, &otGoals, &shots, &shotPercent, timeOnIce, &shiftsPerGame, &faceOffWinPercent);
if (strlen(player) > strPlayer) strPlayer = strlen(player);
if (strlen(timeOnIce) > strTimeOnIce) strTimeOnIce = strlen(timeOnIce);
}
printf("The longest player name is %d characters long\n", strPlayer);
printf("The time on ice vector needs %d characters\n", strTimeOnIce);
fclose(fp);
return 0;
}
int sortStats(int numPlayers, struct stats myPlayers[numPlayers])
{
int mostGoals = 0;
int mostGoalsInd = 0;
int i = 0;
for (i=0;i<numPlayers;i++)
{
if (myPlayers[i].goals > mostGoals)
{
mostGoals = myPlayers[i].goals;
mostGoalsInd = myPlayers[i].ind;
}
}
//printf("%s scored the most goals this season with %d.\n", myPlayers[mostGoalsInd], mostGoals);
return 0;
}
int loadData(int numPlayers, char *fileName)
{
int i = 0;
FILE *fp;
fp = fopen(fileName, "r");
struct stats myPlayers[numPlayers];
while (!feof(fp))
{
fscanf(fp, "%d %s %s %c %d %d %d %d %d %d %d %d %d %d %d %f %s %f %f", &ind, player, team, &pos, &gamesPlayed, &goals, &assists, &points, &plusMinus, &penaltyMins, &ppGoals, &shGoals, &gwGoals, &otGoals, &shots, &shotPercent, timeOnIce, &shiftsPerGame, &faceOffWinPercent);
myPlayers[i].ind = ind;
strcpy(myPlayers[i].player, player);
strcpy(myPlayers[i].team, team);
strcpy(myPlayers[i].pos, pos);
myPlayers[i].gamesPlayed = gamesPlayed;
myPlayers[i].goals = goals;
myPlayers[i].assists = assists;
myPlayers[i].points = points;
myPlayers[i].plusMinus = plusMinus;
myPlayers[i].penaltyMins = penaltyMins;
myPlayers[i].ppGoals = ppGoals;
myPlayers[i].shGoals = shGoals;
myPlayers[i].gwGoals = gwGoals;
myPlayers[i].otGoals = otGoals;
myPlayers[i].shots = shots;
myPlayers[i].shotPercent = shotPercent;
strcpy(myPlayers[i].timeOnIce, timeOnIce);
myPlayers[i].shiftsPerGame = shiftsPerGame;
myPlayers[i].faceOffWinPercent = faceOffWinPercent;
i++;
}
return 0;
}
int main(int argc, char *argv[])
{
int numPlayers;
char myStr[] = "nhl0708.txt";
char myPlayers[1000];
numPlayers = getNumPlayers(myStr);
getLength(numPlayers, myStr);
myPlayers = loadData(numPlayers, myStr);
sortStats(numPlayers, myPlayers);
system("PAUSE");
return 0;
}