Bonjour, je cherche en vain depuis une 10aine d'heures le problème dans les fonctions qui suivent. Avant, voici les quelques explications :
J'ai une fonction main, qui appelle en boucle à chaque ligne une fonction buildEntryFieldList (qui parse la ligne en deux chaines de caractères : nom et valeur), qui elle même appelle insertTailListEntrFi, qui insère ce qui a été obtenu dans une liste.
Le problème : Au final, quand j'utilise ma fonction printList, je me retrouve avec X fois (X étant le nombrede lignes qui ont été lues) la valeur et le nom de la DERNIERE ligne seulement. Voici un screen :
http://img4.hostingpics.net/pics/971...2012155429.png
Les deux lignes commençant par des '#' et les deux lignes qui suivent sont des lignes écrites au niveau de l'insertion dans la liste, et déjà à ce niveau on se rend compte que la valeur qui vient d'être insérée a été aussi insérée à la place de la valeur PRECEDENTE (et de toutes les autres valeurs en fait)
Le résultat de la liste est affichée après, avec le "author" qui est comme on le voit remplacé par des symboles, et toutes les autres lignes similaires.
Voici mon code :
main
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 int main(int argc,char* argv[]) { FILE* fp; char actualCaracter; char* line = (char*) malloc(2000*sizeof(char)); Entry tempEntry; tempEntry.listEntryFields=NULL; initializeEntry(&tempEntry); fp = fopen("test.txt","r"); if(fp==NULL) { printf("FAILED TO OPEN THE FILE\n"); } else { actualCaracter = fgetc(fp); while(actualCaracter != EOF) { if(actualCaracter == '@') { tempEntry.listEntryFields=NULL; /*to make the listEntryField reboot*/ printf("> Entry was found\n"); fscanf(fp,"%s",line); strcpy(tempEntry.type,strtok(line,"{")); strcpy(tempEntry.key,strtok(NULL,",")); printf("Type = %s\nKey = %s\n",tempEntry.type,tempEntry.key); actualCaracter=fgetc(fp); /*to go after the comma and go to the CR*/ fscanf(fp,"%s",line); printf("%s\n",line); getchar(); do { printf("Entryfield was found\n"); tempEntry.listEntryFields = buildEntryFieldList(tempEntry.listEntryFields,line,fp); fseek(fp,-1,SEEK_CUR); /*we go back to the previous caracter, because we are too far and we have to read the line)*/ fscanf(fp,"%s",line); printf("%s\n",line); }while(strcmp(line,"}")!=0); /*a line that has only "}" can only be the last line of the entry*/ printEntryFieldList(tempEntry.listEntryFields); } actualCaracter = fgetc(fp); } fclose(fp); } freeEntry(&tempEntry); return 0; }
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 ListEntryFields buildEntryFieldList(ListEntryFields listEntryFields,char* line,FILE* fp) { int i=0; /*an iteration variable*/ EntryField tempEntryField; char actualCaracter; int verif = 0; char* buffer = (char *) malloc(2000*sizeof(char)); tempEntryField.fieldname = (char *) malloc(2000*sizeof(char)); tempEntryField.value = (char *) malloc(2000*sizeof(char)); strcpy(tempEntryField.fieldname,strtok(line,"=")); do { actualCaracter=fgetc(fp); }while(actualCaracter!='{'); do { actualCaracter=fgetc(fp); buffer[i]=actualCaracter; /*We copy the contents of the entryfield caracter by caracter*/ if(actualCaracter=='\n') { actualCaracter=fgetc(fp); if(actualCaracter=='\t' || actualCaracter=='}') /*we check that there is a '\t' after the '\n', meaning that it's the end of the entryfild, of simply the end of the entry*/ { verif=1; } } i++; }while(verif != 1); /*until the caracter is the CR*/ buffer[i-4]='\0'; /* to delete the "}," at the end of the line*/ strcpy(tempEntryField.value,buffer); getchar(); listEntryFields = insertTailListEntrFi(&tempEntryField,listEntryFields); free(tempEntryField.fieldname); free(tempEntryField.value); free(buffer); return listEntryFields; }
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 ListEntryFields insertTailListEntrFi(EntryField *tempEntryField,ListEntryFields listEntryFields) { EntryFieldElem* newel = (EntryFieldElem *) malloc(sizeof(EntryFieldElem)); EntryFieldElem* p; newel->entryField = *tempEntryField; newel->next = NULL; newel->prev = NULL; if(listEntryFields==NULL) { listEntryFields=newel; } else { p=listEntryFields; while(p->next != NULL) { p=p->next; } p->next = newel; newel->prev = p; printf("PREVIOUS\n#Fieldname : %s\n#Value : %s\n",newel->prev->entryField.fieldname,newel->prev->entryField.value); } printf("Fieldname : %s\nValue : %s\n",newel->entryField.fieldname,newel->entryField.value); return listEntryFields; }
Ainsi que mes structures :
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 void printEntryFieldList(ListEntryFields listEntryFields) { ListEntryFields p; if(listEntryFields == NULL) { printf("Liste vide !!\n"); } else { p=listEntryFields; while(p!=NULL) { printf("Fieldname : %s\n Value : %s\n",p->entryField.fieldname,p->entryField.value); p=p->next; } } }
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 typedef struct { char* fieldname; char* value; }EntryField; typedef struct entrfi{ EntryField entryField; struct entrfi* next; struct entrfi* prev; }EntryFieldElem; /*the list of entryfields*/ typedef EntryFieldElem* ListEntryFields; typedef struct { char* type; char* key; ListEntryFields listEntryFields; }Entry;
Si vous pouviez m'aider, je vous serait graaandement reconnaissants...
Partager