Transformation d'un fichier en liste doublement chainée
Hors ligneBeamo Le 30/12/2007 à 18:06 Profil de Beamo Configuration de Beamo

Bonjour,

Je cherche à transformer un fichier en liste doublement chainée.
Mon fichier contient un mot de moins de 32 caractères par ligne. Et je veux que chaque ligne soit un maillon de ma chaîne.


   1. #include<stdlib.h>
   2. #include<stdio.h>
   3. #include<string.h>
   4. #include<math.h>
   5.
   6. typedef struct liste_ {
   7.     char *mot;
   8.     struct liste_ *precedant;
   9.     struct liste_ *suivant;
  10. }
  11. liste;
  12.
  13. liste *initliste (char *mot) {
  14.
  15.     liste *L;
  16.     L = (liste*)malloc(sizeof(liste));
  17.     L->mot = mot;
  18.     L->precedant = NULL;
  19.     L->suivant = NULL;
  20.     return L;
  21. }
  22.
  23. void addendliste(char *mot, liste *L) {
  24.
  25.     liste *T, *tmp;
  26.     T = (liste*)malloc(sizeof(liste));
  27.     tmp = (liste*)malloc(sizeof(liste));
  28.     tmp = L;
  29.     while (tmp->suivant != NULL)
  30.         tmp = tmp->suivant;
  31.     T->mot = mot;
  32.     T->precedant = tmp;
  33.     T->suivant = NULL;
  34.     tmp->suivant = T;
  35. }
  36.
  37.
  38. int main() {
  39.     liste *L;
  40.     int length = 1;
  41.     char mot[32];
  42.     char tmp[32];
  43.     FILE *lect;
  44.
  45.
  46. /* transformation du dico en liste chainee */
  47.     lect = fopen ("fichier.txt", "r" );
  48.     fgets(mot, 32, lect);
  49.     strcpy (tmp, mot);
  50.     L = initliste(tmp);
  51.     while (fgets(mot, 32, lect)!= NULL) {
  52.         /*strcpy (tmp2, mot);*/
  53.         addendliste(mot, L);
  54.         length++;
  55.     }
  56.     fclose (lect);
  57.
  58.     printf("L: %sL->suivant: %s", L->mot, L->suivant->mot);
  59.     return 0;
  60. }


Mon gros soucis est que à chaque fois que je fais un fgets il écrase un des maillons (car cela pointe vers la même adresse...). J'ai essayé d'utiliser un char xxx[32] en tant que buffeur mais cela ne m'a pas aidé...

Des idées ? :)

Merci,
Beamo
Hors ligneWebmanager2011 Le 05/05/2012 à 13:19 Profil de Webmanager2011 Configuration de Webmanager2011

Tutoriaux plus exercices courigés en language c/c++

jetez un coup d'oeil ici

Vous avez résolu votre problème avec VIC ? Faites-le savoir sur les réseaux sociaux !
Vulgarisation-informatique.com
Cours en informatique & tutoriels