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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAILLE 1000
/////////////DECLARATION DES TYPES///////////////
typedef struct entree
{
char *nom;
char *prenom;
char *initiales;
char *mail;
char *tel;
char *classe;
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 buffer[],LISTE *liste);
void afficher_liste(LISTE *liste);
/////////////////////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[81];
char *nom_fichier=malloc(50*sizeof(char));
LISTE *liste;
liste=creer_liste();
FILE *fichier;
/*printf("entrez le nom du fichier a ouvrir ");
scanf("%s",nom_fichier);*/
nom_fichier="fichier.txt";
fichier = fopen(nom_fichier,"r");
if(fichier!=NULL)
{
while(fgets(buffer,81,fichier)!=NULL)
{
nvl_entree(buffer,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 buffer[],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));
char sep[1];
sep[0]=';';
n_entree->nom=strtok(buffer,sep);
n_entree->prenom=strtok(NULL,sep);
n_entree->initiales=strtok(NULL,sep);
n_entree->mail=strtok(NULL,sep);
n_entree->tel=strtok(NULL,sep);
n_entree->classe=strtok(NULL,sep);
if(liste->t==0)
{
liste->first=n_entree;
}
else
{
ENTREE *courant;
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++;
} |
Partager