bonsoir,

Je bute sur le problème suivant.
Avec libxml, je lis un fichier xml sans aucun problème et souhaiterais initialiser une structure avec le résultat. Le programme plante à l'éxécution.

La ligne avec printf affiche bien la donnée que je veux, elle est donc bien récupérée depuis le fichier xml.
Si je remplace la ligne strcpy par
Code : Sélectionner tout - Visualiser dans une fenêtre à part
strcpy(waypoints[i].wp_nom, "coco");
le code s'exécute sans erreur.


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
static int nb_wp = 0;
 
typedef struct
{
    char wp_nom[5];
    int wp_x;
    int wp_y;
} s_waypoint;
 
s_waypoint *waypoints;
 
void import_wp()
{
    xmlDoc *doc = xmlReadFile("basic.xml", NULL, 0);
    if (doc == NULL)
    {
       g_print("Le fichier xml n'existe pas.\n");
    } else
    {
        xmlNode *xmlroot = xmlDocGetRootElement(doc)->children;
        xmlNode *current = NULL;
        int i = 0;
 
        for (current=xmlroot; current != NULL; current=current->next)
        {
            if (strcmp((char*)current->name, "targetpoint") == 0)
            {
                nb_wp++;
            }
        }
        waypoints = malloc(nb_wp*sizeof(s_waypoint));
       for (current=xmlroot; current != NULL; current=current->next)
        {
            printf("%s\n", (const char*) xmlGetProp(current, (const xmlChar*)"label"));
            strcpy(waypoints[i].wp_nom, (const char*) xmlGetProp(current, (const xmlChar*)"label"));
            i++;
        }
 
        xmlFreeDoc(doc);
        xmlCleanupParser();
    }
}
Le fichier xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
<?xml version="1.0"?>
<Waypoints>
  <targetpoint x="398" y="470" label="MTL"/>
  <targetpoint x="473" y="288" label="LTP"/>
</Waypoints>
Je ne sais plus quoi faire. Un petit peu d'aide serait la bienvenue.
Merci