#include #include #include #include #include #include #include #include #define STRING_MAX 32 struct etudiant { int id; char nom[STRING_MAX]; }; struct cours { int id; char nom[STRING_MAX]; }; struct note { int etudiant_id; int cours_id; int note; }; void add_etudiant(char* name) { int fd = open("etudiants.dat", O_RDWR); int file_size=lseek(fd, 0, SEEK_END); int nb_etudiants = file_size/sizeof(struct etudiant); struct etudiant e; e.id = nb_etudiants; strncpy(e.nom, name, STRING_MAX); write(fd, &e, sizeof(struct etudiant)); close(fd); } void add_cours(char* name) { int fd = open("cours.dat", O_RDWR); int file_size=lseek(fd, 0, SEEK_END); int nb_cours = file_size/sizeof(struct etudiant); struct cours c; c.id = nb_cours; strncpy(c.nom, name, STRING_MAX); write(fd, &c, sizeof(struct cours)); close(fd); } void add_note(int etudiant_id, int cours_id, int note) { int fd = open("notes.dat", O_RDWR); struct note n; n.etudiant_id = etudiant_id; n.cours_id = cours_id; n.note = note; write(fd, &n, sizeof(struct note)); close(fd); } void print_note(struct etudiant *e, struct note *n) { int fd = open("cours.dat", O_RDONLY); if(fd < 0) { perror("Cannot to open etudiants.dat"); abort(); } lseek(fd, n->cours_id*sizeof(struct cours), SEEK_SET); struct cours c; read(fd, &c, sizeof(c)); assert(c.id == n->cours_id); printf("\t%s\t%d\n", c.nom, n->note); } void print_bulletin(struct etudiant *e) { int fd = open("notes.dat", O_RDONLY); if(fd < 0) { perror("Cannot to open etudiants.dat"); abort(); } struct note n; while(read(fd, &n, sizeof(n))>0) { if(n.etudiant_id == e->id) { print_note(e, &n); } } close(fd); } void print_bulletins() { int fd = open("etudiants.dat", O_RDONLY); if(fd < 0) { perror("Cannot to open etudiants.dat"); abort(); } struct etudiant e; while(read(fd, &e, sizeof(e))>0) { printf("%d - %s\n", e.id, e.nom); print_bulletin(&e); } close(fd); } int main(int argc, char**argv) { #if 0 add_etudiant("Hector_le_castor"); add_etudiant("Augustin_le_lamantin"); add_etudiant("Annette_la_crevette"); add_etudiant("Yolande_la_limande"); #endif #if 0 add_cours("Système d'exploitation"); add_cours("Programmation système"); add_cours("Java"); add_cours("Anglais"); add_cours("Traitement du signal"); add_cours("Allemand"); #endif #if 0 add_note(0, 0, 18); add_note(0, 1, 16); add_note(0, 2, 13); add_note(0, 4, 8); add_note(1, 0, 11); add_note(1, 1, 13); add_note(1, 2, 16); add_note(1, 3, 15); add_note(2, 1, 9); add_note(2, 2, 14); add_note(2, 3, 11); add_note(3, 4, 16); add_note(3, 3, 12); add_note(4, 0, 14); add_note(4, 2, 18); add_note(4, 4, 11); add_note(4, 3, 16); add_note(5, 1, 17); add_note(5, 0, 12); add_note(5, 2, 8); add_note(5, 3, 12); #endif print_bulletins(); return EXIT_SUCCESS; }