1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct DATA{
char nom[15];
char prenom[15];
char initial[5];
char email[30];
unsigned int num_tel;
int taille;
char classe[15];
struct DATA *next;
struct DATA *previous;
};
typedef struct DATA ELEVE;
void init_list(ELEVE *debut)
{
debut->num_tel = 0;
debut->taille = 0;
debut->next = NULL;
debut->previous = NULL;
}
void add_eleve(ELEVE *debut, ELEVE *nouveau)
{
if (debut->taille == 0)
{
debut->taille = 1;
debut->next = nouveau;
nouveau->previous = debut;
}
else{
ELEVE *current = debut->next;
int i;
for(i=1;i<debut->taille;i++)
{
current=current->next;
}
current->next = nouveau;
nouveau->previous = current;
debut->taille++;
nouveau->taille = debut->taille;
}
}
ELEVE *buildEleve (char nom[15],char prenom[15],char initial[5],char email[30],unsigned int num_tel,char classe[15])
{
ELEVE *data=malloc(sizeof(ELEVE));
init_list(data);
strcpy(data->nom,nom);
strcpy(data->prenom,prenom);
strcpy(data->initial,initial);
strcpy(data->email,email);
data->num_tel=num_tel;
strcpy(data->classe,classe);
return data;
}
ELEVE *load_fichier(char *path)
{
ELEVE *debut = malloc(sizeof(ELEVE));
init_list(debut); //initialisation de la liste
char *c;
int i;
FILE *file = fopen(path,"r");
if(file==NULL)
{
printf("\n impossible d'ouvrir le fichier \n\n");
return 0;
}
char nom[15];
char prenom[15];
char initial[5];
char email[30];
unsigned int num_tel;
char classe[15];
for(i=0;i<28;i++)
{
fscanf(file,"%[^;];%[^;];%[^;];%[^;];%d;%s\n", nom,prenom,initial,email,&num_tel,classe);
if ((c=strrchr(classe,'\n')) != NULL) *c='\0';
add_eleve(debut,buildEleve(nom,prenom,initial,email,num_tel,classe));
memset (nom, 0, sizeof (nom));
memset (prenom, 0, sizeof (prenom));
memset (initial, 0, sizeof (initial));
memset (email, 0, sizeof (email));
memset (classe, 0, sizeof (classe));
}
fclose (file);
free(file);
free(c);
return debut;
}
void list_delete(ELEVE *list)
{
if (list != NULL)
{
ELEVE *p_tmp = list->next;
while (p_tmp != NULL)
{
ELEVE *p_del = p_tmp;
p_tmp = p_tmp->next;
free(p_del);
}
free(list), list = NULL;
}
}
int main()
{
ELEVE *liste = load_fichier("liste.csv");
list_delete(liste);
return 0;
} |
Partager