Question
IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures
IN C ONLY
As mentioned earlier there are two changes we are going to make from lab 5,
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),
Similar Functions from lab05
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->
Entirely new functions for lab06
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.
#include
struct studentInfo
{
char *name;
int number;
};
typedef struct studentInfo Student;
struct studentList
{
int length;
Student *list;
};
typedef struct studentList List;
Student newStudent(char *string);
void freeStudent(Student student);
void freeStudentArray(Student *studentList, int length);
List *readStudentFile(char *fileName);
void addStudent(List *studentList, char *toAdd);
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)
{
/*
This is the function from last lab that must me changed to meet these specifications
As mentioned earlier there are two changes we are going to make from lab 5,
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.
*/
/* Initialize the student List pointer (allocate memory), allocate enough memory to store 5 students */
List *allStudents = NULL;
char buffer[50];
FILE *fPtr;
int studentCount = 0;
/* Allocate enough memory to store 5 students */
allStudents->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)
{
allStudents->list[studentCount++]=newStudent(buffer);
}
allStudents->length=studentCount--;
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
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