Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions

Question

6 What is the balanced scorecard method?

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

What is job enlargement ?

Answered: 1 week ago

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago

Question

1. Identify the sources for this conflict.

Answered: 1 week ago