Bonjour,

J'écris un programme qui va lire dans un fichier et placer chaque ligne dans un noeud d'une liste chainée. Je parviens à lire ce fichier et à créer la liste avec le bon élément à l'intérieur, mais lorsque j'essaie d'en ajouter un deuxième, le premier élément est écrasé et ainsi de suite (tous les noeuds contiennent la même information). Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
struct test_struct
{
    char* valeur;
    char** tableau;
    struct test_struct *next;
};
FILE* ouvrirFichier(char* fichier);
struct test_struct* create_list(char* ligne);
struct test_struct* add_to_list(char* ligne);
void print_list();
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
//struct test_struct *ptr = NULL;
int main(int argc, char** argv)
{
    //int i = 0, ret = 0;
    struct test_struct *ptr = NULL;
    FILE* fichier = NULL;
    char ligne[121] = {0};
    fichier = ouvrirFichier(argv[1]);
    while(fgets(ligne, 121, fichier))
    {
        ptr = add_to_list(ligne);
        printf("%sn", courant->valeur);
        printf("%sn", ptr -> valeur);
    }
    print_list();
    return 0;
}
struct test_struct* create_list(char* ligne)
{
    //printf("n creating list with headnode as [%d]n",val);
    struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
    char* info[121] = {0};
    char separateurs[] = "[]";
    int j = 0;
    int k = 0;
    char* element;
    if(NULL == ptr)
    {
        printf("n Node creation failed n");
        return NULL;
    }
    for (element = strtok(ligne, separateurs); element; element = strtok(NULL, separateurs))
    {
        if (strcmp(element, " ") != 0 && strcmp(element, "n") != 0)
        {
            info[j] = element;
            //printf("%sn", info[j]);
            //info[j] = element;
            j++;
        }
    }
    k = j;
    ptr-> valeur = info[0];
    ptr -> tableau = malloc(k);
    printf("%s234n", ptr -> valeur);
    for (j = 1; j < k; j++)
    {
        ptr -> tableau[j - 1] = malloc(strlen(info[j]));
        ptr -> tableau[j - 1] = info[j];
        //printf("%s ", tete -> tableau[j - 1]);
    }
    ptr->next = NULL;
    head = curr = ptr;
    return ptr;
}
FILE* ouvrirFichier(char* entree)
{
    FILE* fichier = NULL;
    fichier = fopen(entree, "r");
    if (fichier == NULL) // Le fichier n'a pu être ouvert
    {
        perror("Erreur d'ouverture du fichier d'entrée ");
        exit(1);
    }
    return fichier;
}
struct test_struct* add_to_list(char* ligne) // C'est cette fonction qui pose problème
{
    int j = 0;
    int k = 0;
    //char ligne[121] = {0};
    char* info[121] = {0};
    char* element;
    char separateurs[] = "[]";
    if(NULL == head)
    {
        return (create_list(ligne));
    }
 
    if(NULL == ptr)
    {
        printf("n Node creation failed n");
        return NULL;
    }
    for (element = strtok(ligne, separateurs); element; element = strtok(NULL, separateurs))
    {
        if (strcmp(element, " ") != 0 && strcmp(element, "n") != 0)
        {
            info[j] = element;
            //printf("%sn", info[j]);
            info[j] = element;
            j++;
        }
    }
    k = j;
    ptr-> valeur = info[0];
    ptr -> tableau = malloc(k);
    printf("%s123n", ptr -> valeur);
    for (j = 1; j < k; j++)
    {
        ptr -> tableau[j - 1] = malloc(strlen(info[j]));
        ptr -> tableau[j - 1] = info[j];
    }
    ptr->next = NULL;
    return ptr;
}
Merci