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 113 114 115 116 117 118 119
|
#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 = malloc(sizeof(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 tmp[50];
char *nom[15];
char *prenom[15];
char *initial[5];
char *email[30];
unsigned int *num_tel=malloc(29 * sizeof(unsigned int));
char *classe[15];
for(i=0;i<28;i++)
{
nom[i]=malloc(strlen(tmp) + 1);
prenom[i]=malloc(strlen(tmp) + 1);
initial[i]=malloc(strlen(tmp) + 1);
email[i]=malloc(strlen(tmp) + 1);
classe[i]=malloc(strlen(tmp) + 1);
fscanf(file,"%[^;];%[^;];%[^;];%[^;];%d;%s\n", nom[i],prenom[i],initial[i],email[i],&num_tel[i],classe[i]);
if ((c=strrchr(classe[i],'\n')) != NULL) *c='\0';
add_eleve(debut,buildEleve(nom[i],prenom[i],initial[i],email[i],num_tel[i],classe[i]));
}
fclose (file);
free(file);
for(i=0;i<28;i++)
{
free(nom[i]);
nom[i] = NULL;
free(prenom[i]);
prenom[i] = NULL;
free(initial[i]);
initial[i] = NULL;
free(email[i]);
email[i] = NULL;
free(classe[i]);
classe[i] = NULL;
}
free(nom);
free(prenom);
free(initial);
free(email);
free(num_tel);
free(classe);
free(c);
return debut;
}
int main()
{
ELEVE *liste = malloc(sizeof(ELEVE));
liste = load_fichier("liste.csv");
free(liste);
return 0;
} |
Partager