/* * This source code is only for illustration purpose for project1. * It may not contain complete functions for codes required. * If you find any bug, please email me, thanks! * * Author: Ning Yan * Data: 11/4/2013 */ #include #include #define REC_FILE "students.csv" #define SIZE 100 /* definition of student structure */ typedef struct { int ID; char name[20]; int age; char dept[10]; int grade; }Student; /* student records as global static variable, local variable is also fine global variables automatically initialized to 0*/ static Student records[SIZE]; /* print program main menu */ void print_menu(); /* print record table header */ void print_record_header(); /* print a student record */ void print_student_record(Student* s); void print_all_student_records(); /* functions comments omitted */ void add_student(); void modify_student(); void delete_student(); void list_student_dept(); void average_grade(); int main() { FILE * rec_file; /* load records from csv */ if(rec_file = fopen(REC_FILE, "r")) { int i=0; while(fscanf(rec_file, "%d, %[^,], %d, %[^,], %d\n", // can also use fgets and strtok &records[i].ID, // needs to check file format records[i].name, &records[i].age, records[i].dept, &records[i].grade) != EOF) { i++; } fclose(rec_file); printf("Existing record file loaded!\n"); } else { printf("No record file exists!\n"); } print_all_student_records(); /* process user input until exit */ int c; print_menu(); // getchar() is OK, but have to ignore LINEFEED char '\n' scanf("%d", &c); while( c != 6) { printf("You choose %d\n", c); switch (c) { case 1: add_student(); break; case 2: modify_student(); break; case 3: delete_student(); break; case 4: list_student_dept(); break; case 5: average_grade(); break; default: printf("Invalid choice! Enter again...\n"); } printf("\n"); print_menu(); scanf("%d", &c); } /* save record into csv */ if(rec_file = fopen(REC_FILE, "w")) { int i=0; for ( ; iID, s->name, s->age, s->dept, s->grade); } void print_all_student_records() { print_record_header(); int i; for (i=0; i