I am having trouble with an assignment, it involves reading from files into structures and I'm confused on how to do it here is the function with the parameters I HAVE to use.
// This function will read in size struct players from filename and add these
// the players array. The function will use index to know where to start
// writing the players to in the array.
// Parameters
//
// filename – The name of the input file
// players – a pointer to the array of player structures
// index – The index of the array to start placing players into
// size – The number of players in the input file
// Return - Nothing
void read_from_file(char* filename, Player* players, int index, int size);
This is the function I have to use to read in data from 3 DIFFERENT files that look as such:
Andrew Jackson 129 33 38 30 506
Jeremy Warden 25 24 3 9 493
Jared Welch 130 1 43 27 422
Brandon Splitter 138 38 40 7 587
Joe Gwilliams 150 23 30 25 498
Ali Mohammed 119 43 13 6 598
Dheeraj Johnson 124 79 59 36 506
Bill Clinton 121 65 12 26 449
Jesse James 87 58 8 5 464
John Doe 129 100 0 12 548
I have to read in 3 files that all have 10 players in them for a total of 30 I need to read into the structures. I have not gotten very far I know but I am very confused on what to do and how to approach this, any help would be very much appreciated! Below I have down what I have already done. Please help!! Thanks
//Brady Webb
//lab D
//HW1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct player
{
char Fname[25];
char Lname[25];
int Singles;
int Doubles;
int Triples;
int Homeruns;
int At_Bats;
float Slugging_Percentage;
} Player;
void read_from_file(char* filename, Player* players, int index, int size);
int main(int argc, char* argv[])
{
int size= atoi(*(argv+1));
char* file1 = *(argv+2);
char* file2 = *(argv+3);
char* file3 = *(argv+4);
if (argc<6 || argc>6)
{
printf("Incorrect command line arguments\n");
return 0;
}
return 0;
}
void read_from_file(char*filename, Player* players, int index, int size)
{
FILE *ptr;
int i=0;
if ((ptr=fopen(filename, "r")) == NULL)
{
return 0;
}
while (ptr != EOF)
{
}
}