Bonjour,
Encore une fois, j'aurais SVP besoin d'un peu d'aide...
Mon programme principal instancie une structure du type:
puis appelle une fonction qui lis un fichier .txt afin de compléter les champs du Projet_STRUCT...Code:
1
2
3
4
5
6
7
8 typedef struct { char * Nom_projet; char * Chemin; char * DataFile; int * TypeJeu; int * UseFilterFile; char * FilterFile; } Project_STRUCT;
avecCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 int main (int argc, char ** argv) { /******** Creation des elements necessaires au Projet *************/ Project_STRUCT * Projet = malloc(sizeof(Project_STRUCT)); Projet->Nom_projet = NULL; Projet->Chemin = NULL; Projet->DataFile = NULL; Projet->TypeJeu = NULL; Projet->UseFilterFile = NULL; Projet->FilterFile = NULL; lecture_txt(Projet); }
Le programme ci-dessus ne fonctionne pas car mes variables unused sont détruites en fin de fonction, et du coup, mes Projet->xxx pointent vers une adresse corrompue...Code:
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 void lecture_txt (Project_STRUCT * Projet ) { FILE *Out; Out = fopen ("D:\toto.txt", "r"); char firstline[4096]; char unused1[128]; char unused21[4096]; char unused22[4096]; char unused23[4096]; char unused24[4096]; int unused31=0; int unused32=0; if (Out != NULL) { fgets(firstline, sizeof firstline, Out); fscanf(Out, "%s %s\n", unused1, unused21); Projet->Nom_projet = unused21; fgets(firstline, sizeof firstline, Out); fscanf(Out, " %s %s\n ", unused1, unused22); Projet->Chemin = unused22; fgets(firstline, sizeof firstline, Out); fscanf(Out, " %s %s\n ", unused1, unused23); Projet->DataFile = unused23; fgets(firstline, sizeof firstline, Out); fscanf(Out, " %s %d\n ", unused1, &unused31); Projet->TypeJeu = &unused31; fgets(firstline, sizeof firstline, Out); fscanf(Out, " %s %d\n ", unused1, &unused32); Projet->UseFilterFile = &unused32; .... fclose (Out); }
Le problème c'est que le fscanf veut une variable du type tableau de char... Et moi je dois avoir dans mon Project_STRUCT un char *....
Aussi, je ne vois pas comment faire....
Le programme ci-dessus ne fonctionne pas car mes variables unusedxx sont détruites en fin de fonction, et du coup, mes Projet->xxx pointent vers une adresse corrompue...
Alex