Question: C PROGRAM FGETS & STRTOK I'm currently doing an assignment for school and I am having trouble with this function(MUST USE FGETS AND STRTOK): void

C PROGRAM FGETS & STRTOK

I'm currently doing an assignment for school and I am having trouble with this function(MUST USE FGETS AND STRTOK):

void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines) { 
// Reads the player information from the given file. Information is in the following format: // // [name] [goals] [assists] // Backes 1 5 // Pietrangelo 2 7 // // YOU MAY NOT USE FSCANF IN THIS FUNCTION! Doing so will result in no points. Instead you // must use fgets in combination with the strtok function in order to break the line into // pieces. You can read about strtok online or in your textbook; email your TA with any // questions. // // INPUT: fPtr - A pointer to a file to read from. // goals - An array to be filled with the number of goals per player. // assists - An array to be filled with the number of assists per player. // names - An array to be filled with the names of each player. // size - The number of players in each array. 
} 
I currently have this written. I am segfaulting & pretty sure it's happening when I set goals[i] equal to atoi(token). How can I use strtok and set it equal to a integar? What am I doing wrong? Thanks for the help! 

void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines)

{

char line=malloc(sizeof(char)*MAX_LINE)

char *token;

int i;

for(i = 0; i < numLines ; i++)

{

fgets(line,MAX_LINE, fPtr);

token = strtok(line, " ");

strcpy(names[i], " ");

token = strtok(NULL, " ");

goals[i] = atoi(token);

token = strtok(NULL, " ");

assists[i] = atoi(token);

}

free(line);

}

input file:

John 4 4

Fred 6 8

Joe 1 9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!