From 85fbaa4d9381e435be129aa7bc4ea6a472acb2b2 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sun, 5 Oct 2025 16:28:33 +0200 Subject: Cours du 29 au 3 octobre --- .../tme/tme3-5/exo1/entreeSortieLC.c" | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "semestre 3/structures des donn\303\251es/tme/tme3-5/exo1/entreeSortieLC.c" (limited to 'semestre 3/structures des données/tme/tme3-5/exo1/entreeSortieLC.c') diff --git "a/semestre 3/structures des donn\303\251es/tme/tme3-5/exo1/entreeSortieLC.c" "b/semestre 3/structures des donn\303\251es/tme/tme3-5/exo1/entreeSortieLC.c" new file mode 100644 index 0000000..3adb7fb --- /dev/null +++ "b/semestre 3/structures des donn\303\251es/tme/tme3-5/exo1/entreeSortieLC.c" @@ -0,0 +1,51 @@ +#include "entreeSortieLC.h" +#include +#include +#include + +Biblio* charger_n_entrees(char* nomfic, int n){ + Biblio* bib = creer_biblio(); + FILE* f = fopen(nomfic, "r"); + if (!f) return NULL; + for (int i = 0; i < n; i++){ + char content[256]; + char* tmp = fgets(content, 256, f); + if (!tmp) return NULL; + + char* parsed[3]; + char c[256]; + int k = 0; + int l = 0; + for (int j = 0; j < 256 && content[j] != '\0'; j++){ + if (content[j] == ' '){ + c[k] = '\0'; + parsed[l++] = strdup(c); + k = 0; + } else { + c[k++] = content[j]; + } + } + inserer_en_tete(bib, atoi(parsed[0]), parsed[1], parsed[2]); + } + if (fclose(f) != 0) return NULL; + return bib; +} + +void enregistrer_biblio(Biblio *b, char* nomfic){ + FILE* f = fopen(nomfic, "w"); + if (!f) return; + Livre* l = b->L; + while (l){ + int ln1 = strlen(l->titre); + int ln2 = strlen(l->auteur); + int res = fwrite(&(l->num), sizeof(int), 1, f); + if (res != 1) return; + res = fwrite(l->titre, sizeof(char), ln1, f); + if (res != ln1) return; + res = fwrite(l->auteur, sizeof(char), ln2, f); + if (res != ln2) return; + if (fwrite("\n", sizeof(char), 1, f) != 1) return; + l = l->next; + } + if (fclose(f) != 0) printf("erreur lors de la fermeture\n"); +} -- cgit v1.2.3