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 struct livre* premier;
static struct livre* fin;
struct livre{
char* titre;
char* auteur;
short int age;
struct livre * suivant;
}__attribute__((packed));
void increment_element(struct livre* elem){
*elem=*elem->suivant;
}
void ajout(char * t, char* a, int y){
struct livre* s;
s = malloc(sizeof(struct livre));
s->titre = t;
s->auteur = a;
s->age = y;
s->suivant = NULL;
if(premier==NULL){
premier=s;
fin = premier;
return;
}
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 main(int argc, char *argv[]){
//struct livre l1 = {"Le bruit et la fureur", "William Faulkner", 1, NULL};
//struct livre l2 = {"Demande à la poussière", "John Fante", 1, NULL};
//struct livre l3 = {"À la recherche du temps perdu", "Albert Camus", 1, NULL};
//struct livre l4 = {"Madame Bovary", "Gustave Flaubert", 1, NULL};
//struct livre l5 = {"La Divine Comédie", "Léon Tolstoï", 1, NULL};
premier = NULL;
ajout("À la recherche du temps perdu | ","Albert Camus\n",10);
ajout("Demande à la poussière | ", "John Fante\n", 12);
ajout("À la recherche du temps perdu | ", "Albert Camus\n", 2);
ajout("Madame Bovary | ", "Gustave Flaubert\n", 200);
ajout("La Divine Comédie | ","Léon Tolstoï\n",30);
struct livre* elem = malloc(sizeof(struct livre));
elem = premier;
printf("La bibliothèque contient les elements suivants (liste chainé): \n");
printf("------------------DEBUT----------------\n");
while(elem->suivant!=NULL){
printf("%s\n",elem->titre);
elem=elem->suivant;
//increment_element(elem);
}
printf("%s\n",elem->titre);
printf("-----------------FIN----------------\n");
printf("-----------Debut de sauvegarde dans le fichier bibliotheque_sauvegarde_raphael.txt------------\n\n");
FILE *fichier_sauvegarde;
fichier_sauvegarde = fopen("/home/raphael/projetc/bibliotheque_sauvegarde_raphael.txt", "w");
struct livre* incr = malloc(sizeof(struct livre));
incr = premier;
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");
printf("-----------Debut de lecture dufichier bibliotheque_sauvegarde_raphael.txt------------\n\n");
FILE* fichier_lecture = fopen("/home/raphael/projetc/bibliotheque_sauvegarde_raphael.txt", "r");
int c = fgetc(fichier_lecture);
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);
return;
}
Revise this Paste