#include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct { char nom[30]; double prix; int quantite; } Produit; typedef struct Element { Produit produit; struct Element* suiv; } Element; void freeList(Element* elt) { Element* tmp = NULL; while (elt != NULL) { tmp = elt; elt = elt->suiv; free(tmp); } } Element* chargerListe(const char* filename) { FILE* fichier = NULL; Produit produit; Element* q = NULL; Element* p = NULL; int res = 0; fichier = fopen(filename, "rb"); if (!fichier) return NULL; while (!feof(fichier) && !ferror(fichier)) { res = fread(&produit, sizeof(Produit), 1, fichier); if (res <= 0 && feof(fichier)) break; if (res != 1) { printf("Error while loading the file: %s!" " Expected to read 1 product but read %i.\n", filename, res); freeList(q); fclose(fichier); return NULL; } q = malloc(1 * sizeof(Element)); q->produit = produit; q->suiv = p; p = q; } fclose(fichier); return p; } int save(const Element* elt, const char* filename) { FILE* fichier = NULL; int res = 0; fichier = fopen(filename, "w"); if (!fichier) return 0; while (elt != NULL) { res = fwrite(&elt->produit, sizeof(Produit), 1, fichier); if (res != 1) return 0; elt = elt->suiv; } fclose(fichier); return 1; } void afficher(const Element* elt) { while (elt != NULL) { printf("%s\t%f\t%i\n",elt->produit.nom, elt->produit.prix, elt->produit.quantite); elt = elt->suiv; } } Element* createExample(void) { Element* table = NULL; Element* next = NULL; next = malloc(1 * sizeof(Produit)); strcpy(next->produit.nom, "tata"); next->produit.prix = 40; next->produit.quantite = 74; next->suiv = NULL; table = malloc(1 * sizeof(Produit)); strcpy(table->produit.nom, "toto"); table->produit.prix = 10; table->produit.quantite = 29; table->suiv = next; return table; } int main(void) { const char* filename = "database.txt"; Element* table = NULL; Element* example = createExample(); afficher(example); save(example, filename); freeList(example); printf("Chargement de la Base de donnée en cours...\n"); table = chargerListe(filename); if (table) { printf("Contenu de la base:\n"); afficher(table); } else printf("Echec du chargement du fichier: %s\n", filename); freeList(table); return 0; }
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <malloc.h> typedef struct { char nom[30]; double prix; int quantite; } Produit; typedef struct Element { Produit produit; struct Element* suiv; } Element; void freeList(Element* elt) { Element* tmp = NULL; while (elt != NULL) { tmp = elt; elt = elt->suiv; free(tmp); } } Element* chargerListe(const char* filename) { FILE* fichier = NULL; Produit produit; Element* q = NULL; Element* p = NULL; int res = 0; fichier = fopen(filename, "rb"); if (!fichier) return NULL; while (!feof(fichier) && !ferror(fichier)) { res = fread(&produit, sizeof(Produit), 1, fichier); if (res <= 0 && feof(fichier)) break; if (res != 1) { ferror(fichier); printf("Error while loading the file: %s!" " Expected to read 1 product but read %d.\n", filename, res); freeList(q); fclose(fichier); return NULL; } q = malloc(1 * sizeof(Element)); q->produit = produit; q->suiv = p; p = q; } fclose(fichier); return p; } int save(const Element* elt, const char* filename) { FILE* fichier = NULL; int res = 0; fichier = fopen(filename, "w"); if (!fichier) return 0; while (elt != NULL) { res = fwrite(&elt->produit, sizeof(Produit), 1, fichier); if (res != 1) return 0; elt = elt->suiv; } fclose(fichier); return 1; } void afficher(const Element* elt) { while (elt != NULL) { printf("%s\t%f\t%d\n",elt->produit.nom, elt->produit.prix, elt->produit.quantite); elt = elt->suiv; } } Element* createExample(void) { Element* table = NULL; Element* next = NULL; next = malloc(1 * sizeof(Produit)); strcpy(next->produit.nom, "tata"); next->produit.prix = 40; next->produit.quantite = 74; next->suiv = NULL; table = malloc(1 * sizeof(Produit)); strcpy(table->produit.nom, "toto"); table->produit.prix = 10; table->produit.quantite = 29; table->suiv = next; return table; } int main(void) { const char* filename = "dataBase.txt"; Element* table = NULL; Element* example = createExample(); afficher(example); save(example, filename); freeList(example); printf("Chargement de la Base de donnée en cours...\n"); table = chargerListe(filename); if (table) { printf("Contenu de la base:\n"); afficher(table); } else printf("Echec du chargement du fichier: %s\n", filename); freeList(table); return 0; }