Question
C PROGRAMMING ONLY The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that
C PROGRAMMING ONLY
The file you read into data structures can be any length.
studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list.
Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all the way through our program, we will create a struct that stores the total number of current students in the list and the list itself. To do this, you can reuse some of the code YOU wrote in lab05 and implement the ability to read files of any size.
You will write the following functions (newStudent, freeStudent, and freeStudentArray will be the exact same),
Functions
newStudent - Return a new student from a single string passed "student name, number"
freeStudent - Free any of the allocated memory from within the student struct.
freeStudentArray - Free all memory associated with any memory allocated in the array.
readStudentFile - Read an entire text file into a single studentList struct. The struct is a pointer and reallocates memory as needed (make sure you actually opened the file). You can reallocate memory for studentList->
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.
//////
%%file lab06/main.c
#include "lab06.h" /**/ 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; }
////
CODE GOES HERE
%%file lab06/students.c #include "lab06.h" /* Creating a new struct from a string in the form "name, studentNumber" */ Student newStudent(char *string) { Student new; /* Hint: use strtok to split the string using a comma as the delimiter */ return new; } /* Free any memory allocated inside of the student struct */ void freeStudent(Student studentToFree) { } /* 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 dynamically allocated array */ List *readStudentFile(char *fileName) { List *allStudents = NULL; /* Initialize the student List pointer (allocate memory), allocate enough memory to store 5 students */ /* Open the file, read content with fgets, close file */ return allStudents; } /*** * Entirely new functions for lab06 ***/ /* 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
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