#include #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; }; struct cours *cours = NULL; int nb_cours = 0; 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) { struct cours *c = &cours[n->cours_id]; 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) { int fd = open("cours.dat", O_RDONLY); if(fd < 0) { perror("Cannot to open etudiants.dat"); abort(); } struct stat stat; if(fstat(fd, &stat) < 0) { perror("fstat failed"); abort(); } nb_cours = stat.st_size / sizeof(struct cours); cours = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if(cours == MAP_FAILED) { perror("mmap failed"); abort(); } print_bulletins(); munmap(cours, stat.st_size); close(fd); return EXIT_SUCCESS; }