Compare commits

...

4 Commits

Author SHA1 Message Date
5d13eb0e38 Updated the file head 2020-08-02 19:49:41 +07:00
ff0e21d42b Preform the linked list nodes
Use free to clean up the memory
2020-08-02 19:42:42 +07:00
909f55f621 Updated function 2020-08-02 19:36:32 +07:00
632b863ff9 Remove unused comments 2020-08-02 19:32:17 +07:00

View File

@ -32,6 +32,11 @@ typedef struct person_tag PERSON;
typedef struct course_tag COURSE;
typedef struct student_tag STUDENT;
// util functions
void display_menu();
void print_welcome();
void print_student(STUDENT *student);
// core functions
void display_students();
void search_student();
@ -45,11 +50,6 @@ void quite();
void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[MAX_NO_OF_UNITS]);
int count();
// util functions
void display_menu();
void read_student_to_data();
void print_welcome();
int main(void)
{
// variable use for menu selected
@ -57,6 +57,9 @@ int main(void)
print_welcome();
// init the read data from the file
read_file();
while (1)
{
display_menu();
@ -99,7 +102,7 @@ void display_students()
{
printf("\n");
read_student_to_data();
read_file();
STUDENT *student;
@ -191,88 +194,6 @@ void add_student(char student_name[20], char student_id[10], char course_name[20
}
}
void read_student_to_data()
{
// init head to null
head = NULL;
FILE *file;
file = fopen(FILE_STUDENT_DATA_PATH, "r");
if (file == NULL)
{
printf("cannot read file: %s", FILE_STUDENT_DATA_PATH);
exit(EXIT_FAILURE);
}
char student_name[20];
char student_id[10];
char course_name[20];
int no_of_units;
int marks[4];
int i;
while (!feof(file))
{
char no[BUFFER_SIZE];
fgets(student_name, sizeof student_name, file);
fgets(student_id, sizeof student_id, file);
fgets(course_name, sizeof course_name, file);
fgets(no, sizeof no, file);
i = 0;
while (student_name[i] != '\n')
{
i++;
}
student_name[i] = '\0';
i = 0;
while (student_id[i] != '\n')
{
i++;
}
student_id[i] = '\0';
i = 0;
while (course_name[i] != '\n')
{
i++;
}
course_name[i] = '\0';
// printf("Student Name: %s\n", student_name);
// printf("Student ID: %s\n", student_id);
// printf("Course Name: %s\n", course_name);
// printf("No of units: %s", no);
no_of_units = atoi(no);
for (int j = 0; j < no_of_units; j++)
{
char mark[BUFFER_SIZE];
fgets(mark, sizeof mark, file);
sscanf(mark, "%d", &marks[j]);
}
// printf("Marks: [ ");
// for (int x = 0; x < no_of_units; x++)
// {
// printf("%d ", marks[x]);
// }
// printf("]\n\n");
// add into linked list
add_student(student_name, student_id, course_name, no_of_units, marks);
}
fclose(file);
}
int count()
{
int n = 1;
@ -331,7 +252,8 @@ void update_file()
printf("Enter no of units: ");
scanf("%d", &no_of_units);
if (no_of_units > MAX_NO_OF_UNITS) {
if (no_of_units > MAX_NO_OF_UNITS)
{
printf("you cannot input the units bigger than %d\n", MAX_NO_OF_UNITS);
getchar();
goto again;
@ -365,11 +287,88 @@ void update_file()
fclose(file);
printf("\nSaved");
// reload data into linked list again
read_file();
}
void read_file()
{
printf("\nNot implement yet!");
// free the nodes
// because it can be use in memory
// we need to clear it first
// before we re-initailize the new data
STUDENT *temp;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
FILE *file;
file = fopen(FILE_STUDENT_DATA_PATH, "r");
if (file == NULL)
{
printf("cannot read file: %s", FILE_STUDENT_DATA_PATH);
exit(EXIT_FAILURE);
}
char student_name[20];
char student_id[10];
char course_name[20];
int no_of_units;
int marks[4];
int i;
while (!feof(file))
{
char no[BUFFER_SIZE];
fgets(student_name, sizeof student_name, file);
fgets(student_id, sizeof student_id, file);
fgets(course_name, sizeof course_name, file);
fgets(no, sizeof no, file);
i = 0;
while (student_name[i] != '\n')
{
i++;
}
student_name[i] = '\0';
i = 0;
while (student_id[i] != '\n')
{
i++;
}
student_id[i] = '\0';
i = 0;
while (course_name[i] != '\n')
{
i++;
}
course_name[i] = '\0';
no_of_units = atoi(no);
for (int j = 0; j < no_of_units; j++)
{
char mark[BUFFER_SIZE];
fgets(mark, sizeof mark, file);
sscanf(mark, "%d", &marks[j]);
}
// add into linked list
add_student(student_name, student_id, course_name, no_of_units, marks);
}
fclose(file);
}
void quite()