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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAILLE 1000
/////////////DECLARATION DES TYPES///////////////
typedef struct entree
{
char nom[15];
char prenom[15];
char initiales[10];
char mail[35];
char tel[10];
char classe[15];
struct entree *next;
} ENTREE;
typedef struct liste
{
int t;
struct entree *first;
} LISTE;
/////////////PROTOTYPES DE FONCTIONS/////////////
LISTE *lecture_fichier();
LISTE *creer_liste();
void nvl_entree(char nom[15],char prenom[15],char initiales[10],char mail[35],char tel[10],char classe[15],LISTE *liste);
void afficher_liste(LISTE *liste);
void supprime_list(LISTE *list);
/////////////////////FONCTIONS///////////////////
LISTE *creer_liste()
{
LISTE *n_liste = malloc(sizeof(LISTE));
n_liste->t=0;
n_liste->first=NULL;
return n_liste;
}
LISTE *lecture_fichier() // Parcours le fichier txte ligne par ligne et ajoute les entrées dans la liste via la fonction nvl_entree
{
char buffer[256];
char nom[15];
char prenom[15];
char initiales[10];
char mail[35];
char tel[10];
char classe[15];
char nom_fichier[]="fichier.txt";
LISTE *liste=creer_liste();
FILE *fichier;
char *t,*c;
/*printf("entrez le nom du fichier a ouvrir ");
scanf("%s",nom_fichier);*/
fichier = fopen(nom_fichier,"r");
if(fichier!=NULL)
{
while(fgets(buffer,sizeof(buffer),fichier)!=NULL)
{
if((t = strtok(buffer, ";")) == NULL) {
printf("\n !!! parse error line !!!\n");
}
else {
strcpy(nom, t);
}
if((t = strtok(NULL, ";")) == NULL) {
printf("\n !!! parse error line !!!\n");
}
else {
strcpy(prenom, t);
}
if((t = strtok(NULL, ";")) == NULL) {
printf("\n !!! parse error line !!!\n");
}
else {
strcpy(initiales, t);
}
if((t = strtok(NULL, ";")) == NULL) {
printf("\n !!! parse error line !!!\n");
}
else {
strcpy(mail, t);
}
if((t = strtok(NULL, ";")) == NULL) {
printf("\n !!! parse error line !!!\n");
}
else {
strcpy(tel, t);
}
if((t = strtok(NULL, ";")) == NULL) {
printf("\n !!! parse error line !!!\n");
}
else {
strcpy(classe, t);
if ((c=strrchr(classe, '\n')) != NULL) *c='\0';
}
nvl_entree(nom,prenom,initiales,mail,tel,classe,liste);
// printf("%s\n",liste->first->nom); // CE PRINTF AFFICHE UN NOM DIFFERENT A CHAQUE PASSAGE DANS LA BOUCLE -> PQ ???? :'(
}
fclose(fichier);
}
else
{
printf("\n ! erreur de lecture du fichier ! ");
}
return liste;
}
void nvl_entree(char nom[15],char prenom[15],char initiales[10],char mail[35],char tel[10],char classe[15],LISTE *liste) // Ajoute les lignes du fichier dans la liste chainnée => une ligne = une entree (un noeud) de la liste.
{
ENTREE *n_entree=malloc(sizeof(ENTREE));
n_entree->next=NULL;
strcpy(n_entree->nom,nom);
strcpy(n_entree->prenom,prenom);
strcpy(n_entree->initiales,initiales);
strcpy(n_entree->mail,mail);
strcpy(n_entree->tel,tel);
strcpy(n_entree->classe,classe);
if(liste->t==0)
{
liste->first=n_entree;
}
else
{
ENTREE *courant=liste->first; //On part de la premiere entrée.
while(courant->next!=NULL) //On avance dans la liste jusqu'à arriver à la dernière entrée.
{
courant=courant->next;
}
courant->next=n_entree;//lorsqu'on attent le dernier élément , on fait pointer la champ next de la dernier entree vers la nouvelle entrée
}
liste->t++;
}
void afficher_liste(LISTE *liste)
{
int t=1;
if(liste->t==0)
{
printf("Liste vide");
}
else
{
ENTREE *courant=liste->first;
printf("Entree 0: %s %s %s %s %s %s\n",courant->nom,courant->prenom,courant->initiales,courant->mail,courant->tel,courant->classe);
courant=courant->next;
while(courant!=NULL)
{
printf("Entree %d: %s %s %s %s %s %s\n",t,courant->nom,courant->prenom,courant->initiales,courant->mail,courant->tel,courant->classe);
courant=courant->next;
t++;
}
}
}
void supprime_list(LISTE *list)
{
if (list != NULL)
{
ENTREE *p_tmp = list->first;
while (p_tmp != NULL)
{
ENTREE *p_del = p_tmp;
p_tmp = p_tmp->next;
free(p_del);
}
free(list), list = NULL;
}
}
//////////////////////MAIN///////////////////////////
int main()
{
LISTE *liste;
liste=lecture_fichier();
afficher_liste(liste);
supprime_list(liste);
return 0;
} |
Partager