Psst.. new poll here.
[email protected] web/email now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as C by registered user raphael ( 2 years ago )
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
//auteur : Raphael De Oliveira
static char * titre = "/home/raphael/projetc/bibliotheque_sauvegarde_raphael.txt";
struct livre{
char* titre;
char* auteur;
short int age;
struct livre * suivant;
}__attribute__((packed));
void increment_element(struct livre* elem){
*elem=*elem->suivant;
}
static struct livre* ajout(char * t, char* a, int y){
static struct livre* premier;
static struct livre* fin;
struct livre* s;
s = malloc(sizeof(struct livre));
s->titre = malloc(sizeof(char*));
s->titre = t;
s->auteur = malloc(sizeof(char*));
s->auteur = a;
s->age = malloc(sizeof(int));
s->age = y;
s->suivant = malloc(NULL);
s->suivant = NULL;
if(premier==NULL){
premier=s;
fin = premier;
return premier;
}
fin->suivant = s;
fin = s;
}
void sauvegarde_livre(FILE* fichier, struct livre* l){
int c;
for(int i=0; i<strlen(l->titre); i++){
printf("%c",(int)l->titre[i]);
c = fputc((int)l->titre[i], fichier);
}
for(int i=0; i<strlen(l->auteur); i++){
printf("%c",(int)l->auteur[i]);
c = fputc((int)l->auteur[i], fichier);
}
}
void affichage_bibliotheque(struct livre * elem){
struct livre *transi;
transi = elem;
printf("------------------DEBUT----------------\n");
while(transi->suivant!=NULL){
printf("%s%s",transi->titre,transi->auteur);
transi=transi->suivant;
}
printf("%s%s",transi->titre,transi->auteur);
printf("-----------------FIN----------------\n");
}
static struct livre* initialisation(){
struct livre* elem = malloc(sizeof(struct livre));
elem = ajout("À la recherche du temps perdu | ","Albert Camus\n",10);
return elem;
}
void sauvegarde(struct livre* incr){
FILE *fichier_sauvegarde;
fichier_sauvegarde = fopen(titre, "w");
printf("\n---------Debut de sauvegarde dans le fichier bibliotheque_sauvegarde_raphael.txt------------\n");
while(incr->suivant!=NULL){
sauvegarde_livre(fichier_sauvegarde,incr);
//incr=incr->suivant;
increment_element(incr);
}
sauvegarde_livre(fichier_sauvegarde,incr);
printf("\n---------Fin de sauvegarde dans le fichier bibliotheque_sauvegarde_raphael.txt------------\n");
fclose(fichier_sauvegarde);
printf("Le fichier bibliotheque_sauvegarde_raphael.txt a bien été enregistré avec succés\n");
}
void lire_fichier(){
FILE* fichier_lecture = fopen(titre, "r");
int c = fgetc(fichier_lecture);
printf("\n---------Debut de lecture du fichier bibliotheque_sauvegarde_raphael.txt------------\n");
while(c!=-1){
printf("%c",(char)c);
c=fgetc(fichier_lecture);
}
printf("\n---------Fin de lecture du fichier bibliotheque_sauvegarde_raphael.txt------------\n");
fclose(fichier_lecture);
}
void main(int argc, char *argv[]){
struct livre* elem = initialisation();
struct livre* incr = malloc(sizeof(struct livre));
incr = elem;
ajout("Madame Bovary | ", "Gustave Flaubert\n", 200);
ajout("Demande à la poussière | ", "John Fante\n", 12);
ajout("La Divine Comédie | ","Léon Tolstoï\n",30);
ajout("Le bruit et la fureur | ", "William Faulkner\n", 2);
printf("La bibliothèque contient les elements suivants (liste chainé): \n");
affichage_bibliotheque(elem);
sauvegarde(incr);
lire_fichier();
return;
}
Revise this Paste