Compare commits
No commits in common. "5d13eb0e385900a7f82e803ce98f481af03c8179" and "aabb594c70b28238a62529537e4f20d059202e1e" have entirely different histories.
5d13eb0e38
...
aabb594c70
181
work1/single.c
181
work1/single.c
@ -32,11 +32,6 @@ 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();
|
||||
@ -50,6 +45,11 @@ 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,9 +57,6 @@ int main(void)
|
||||
|
||||
print_welcome();
|
||||
|
||||
// init the read data from the file
|
||||
read_file();
|
||||
|
||||
while (1)
|
||||
{
|
||||
display_menu();
|
||||
@ -102,7 +99,7 @@ void display_students()
|
||||
{
|
||||
printf("\n");
|
||||
|
||||
read_file();
|
||||
read_student_to_data();
|
||||
|
||||
STUDENT *student;
|
||||
|
||||
@ -194,6 +191,88 @@ 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;
|
||||
@ -248,12 +327,11 @@ void update_file()
|
||||
printf("Enter course name: ");
|
||||
scanf("%s", &course_name);
|
||||
|
||||
again:
|
||||
again:
|
||||
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;
|
||||
@ -287,88 +365,11 @@ again:
|
||||
fclose(file);
|
||||
|
||||
printf("\nSaved");
|
||||
|
||||
// reload data into linked list again
|
||||
read_file();
|
||||
}
|
||||
|
||||
void read_file()
|
||||
{
|
||||
// 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);
|
||||
printf("\nNot implement yet!");
|
||||
}
|
||||
|
||||
void quite()
|
||||
|
Loading…
Reference in New Issue
Block a user