Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started