Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C ONLY . /* new function concepts */ addStudent - A wrapper function to add a student to the studentList->list. If there is not

In C ONLY .

/* new function concepts */

addStudent - A "wrapper" function to add a student to the studentList->list. If there is not enough memory, you need to allocate more memory in increments of 5 students.

freeAllStudents - Free all memory inside a list of structs (including all memory in each struct).

Hint 1: Use fgets() to read from a file into a buffer before creating a student. Continue to read from the file until fgets returns null (while(fgets != null)).

Hint 2: Use strtok to split the string passed to newStudent.

Hint 3: atoi converts a string to integer.

Hint 4: Remember to allocate memory for your studentList before assigning pointers.

/* Typedef creates an alias data type named "Student" to student student name and number. */

struct studentInfo

{

char *name;

int number;

};

typedef struct studentInfo Student;

/* Notice how you can use "Student" instead of "struct studentInfo" after the typedef */

struct studentList

{

int length;

Student *list;

};

typedef struct studentList List;

/* Creating a new struct from a string in the form "name, studentNumber" */

Student newStudent(char *string);

/* Free the memory inside of the struct */

void freeStudent(Student student);

/* Free all associated memory of a list of structs (including all memory in each struct)*/

void freeStudentArray(Student *studentList, int length);

/* Read an entire file and return a student list object variable */

List *readStudentFile(char *fileName);

/* Allocates new memory if required, then adds a student to the end of the list */

void addStudent(List *studentList, char *toAdd);

/* Free all memory inside a list of structs (including all memory in each struct)*/

void freeAllStudents(List *studentList);

/************************************************************************************************************************************************/

int main(int argc, char *argv[]) {

List *all = NULL;

int i;

if (argc < 1) {

printf("No file name was specified... Now exiting ");

exit(0);

} else {

all = readStudentFile(argv[1]);

}

printf("Now printing the student list before freeing the allocated memory ");

for (i = 0; i < all->length; i++) {

printf("\t%d: %s, %d ", i, all->list[i].name, all->list[i].number);

}

/* Free memory */

freeAllStudents(all);

free(all);

return 0;

}

/************************************************************************************************************************************************/

/* Creating a new struct from a string in the form "name, studentNumber" */

Student newStudent(char *string)

{

Student new;

char *token = strtok(string, ",");

int i = 0;

while (token != NULL) /*Loops as long as Token is not empty*/

{

if (i % 2 == 0)

{

//allocate memory for name

new.name = (char*)malloc(strlen(token)+1 * sizeof(char)); /

strcpy(new.name, token);

}

else

{

new.number = atoi(token);

}

token = strtok(NULL, ",");

++i;

}

return new;

}

/* Free any memory allocated inside of the student struct */

void freeStudent(Student studentToFree)

{

free(studentToFree.name);

}

/* Free all associated memory of a list of structs (including all memory in each struct)*/

void freeStudentArray(Student *studentList, int length)

{

int i;

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

{

freeStudent(studentList[i]);

}

}

/* Read an entire file and return a dynamically allocated array */

List *readStudentFile(char *fileName)

{

/* Initialize the student List pointer (allocate memory), allocate enough memory to store 5 students */

/* Open the file, read content with fgets, close file */

List *allStudents = NULL;

char buffer[50];

FILE *fPtr;

int studentCount = 0;

/* Allocate enough memory to store 5 students */

list = (Student*)malloc(5 * sizeof(Student));

/* Open the file in read mode and see if it exists */

fPtr = fopen("testFile.txt", "r");

if (fPtr == NULL)

{

printf("Error opening input file ");

return NULL;

}

while (fgets(buffer,50,fPtr) != NULL)

{

list[studentCount++]=newStudent(buffer);

}

return allStudents;

}

/* Functions to Be Completed */

/* Allocate more memory for the list when the memory is full, then add to the back of the list */

void addStudent(List *studentList, char *toAdd)

{

/* Hint: use the length studentList->length */

}

/* Free all memory inside a list of structs (including all memory in each struct)*/

void freeAllStudents(List *studentList)

{

/* Hint: this function can be written easily in a couple lines (reuse other code) */

}

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

What three issues have engaged developmental psychologistspg15

Answered: 1 week ago