Bien le bonjour tout le monde!

Alors voilà, j'ai tenté de créer une petite application simple afin de démarrer avec les listes chaînées, j'aimerai bien que vous y jetiez un oeil car j'ai dû bidouiller mon code pour qu'il fonctionne correctement. Notamment lorsque l'on ajoute des contacts à ma liste, j'ai du bidouiller car apparemment mon adresse de début (deb) posait problème. On pouvait le vérifier lors de la consultation, le premier contact était toujours un contact null (rien en guise de paramètres sous mandriva avec gcc, et il affichait des caractères et chemins d'accès avec CodeBlocks).
J'utilise un fichier projet.dat afin de sérialiser mes contacts pour pouvoir les lire par la suite.
Je vous laisse voyager à l'intérieur de ce petit monde :

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct contact
{
	char nom [22];
	char prenom [22];
	char adressenum [42];
	char localitecp [32];
	char email [22];
	struct contact *suivant;
};
 
int main(){
    //Déclaration du fichier d'entrée et de résultat
    	FILE *fdat;
	fdat = fopen("projet.dat", "r+"); //+r permet d'écrire et lire le meme fichier sans supprimer l'ancien
 
    	int proposition();
    	void consultation();
 
	//Déclaration des variables et structures.
	int choix, compteur=0, i,n=0;
    	char aide[22], x[1]; //variable d'aide à l'utilisation de la fct gets()
	typedef struct contact contact;
 
    // typedef on définit la structure contact sous l'alias "contact", comme un nouveau type.
	contact *deb;
	contact *courant;
	contact *suivant;
	contact *nouveau;
	//déclaration de la liste chaînee, avec un élément de début 'courant'
	courant=malloc(sizeof(contact));
	//on sauve l'adresse de l'élément de début de la liste.
	deb=courant;
 
	//Lecture anticipée
       	fscanf(fdat,"%s",&aide);
	while(!feof(fdat))
	{
 
		suivant=malloc(sizeof(contact));
		(*courant).suivant=suivant;
 
      	  	compteur++;
 
		strcpy(courant->nom,aide);
          	fscanf(fdat,"%s",&courant->prenom);
            	fscanf(fdat,"%s",&courant->adressenum);
            	fscanf(fdat,"%s",&courant->localitecp);
            	fscanf(fdat,"%s",&courant->email);
 
		fscanf(fdat,"%s",&aide);
		if (feof(fdat))
		{
			(*courant).suivant=NULL;
		}
		else
		{
			courant=suivant;
		}
     	} 
 
	choix=proposition();
  	while (choix != 0){ //while de lecture, tq on entre pas 0, le pgm ne cesse de fonctionner
 
		switch (choix){
 
			case 1: //Consultation du carnet
 
                	consultation();
                	printf("Il y a %d contact(s) dans votre repertoire.\n", compteur);
                	if(compteur!=0)//ne rien afficher s'il est vide.
			{
				courant = deb;
                		n=1;
				courant=courant->suivant;
                        	while(courant->suivant!=NULL)
                        	{
					printf("Contact %d :\n-----------\n",n);
                            		printf ("Nom : %-20s ", courant->nom);
                            		printf ("Prenom : %-20s\n", courant->prenom);
                            		printf ("Adresse : %-40s ", courant->adressenum);
                            		printf ("%-30s\n", courant->localitecp);
                            		printf ("Mail : %-20s\n", courant->email);
                            		printf ("\n");
 
                            		courant=courant->suivant;
                            		n++;
                		}
				printf("Contact %d :\n-----------\n",n);
                            	printf ("Nom : %-20s ", courant->nom);
                            	printf ("Prenom : %-20s\n", courant->prenom);
                            	printf ("Adresse : %-40s ", courant->adressenum);
                            	printf ("%-30s\n", courant->localitecp);
                            	printf ("Mail : %-20s\n", courant->email);
                            	printf ("\n");
 
			}
                        printf("-------------------------------------\n");
                        //le pgm est en pause.
			system("pause");
 
                	break;
 
			case 2://Ajout d"un contact
 
			/*if(compteur==1)
			{*/
               			nouveau = malloc(sizeof(contact));
				courant->suivant=nouveau;
				courant=nouveau;
 
				courant->suivant=NULL;
 
				printf("   __________________________________________  \n");
                		printf("*/             *   *           *            \\*\n");
                		printf("*|            * *     *** * * ***            |*\n");
                		printf("*|            ***  *  * * * *  *             |*\n");
               	 		printf("*|            * *  *  *** ***  **            |*\n");
                		printf("*|            * * *                          |*\n");
           		     	printf("*|-------------------------------------------|*\n");
                		printf("\n");
                		printf ("Entrez son nom: (20) ");
                		gets (x);// /n lu.
				//gets (courant->nom);
				scanf("%s",&courant->nom);
				printf ("Entrez son prenom: (20) ");
				gets(x);
				//gets (courant->prenom);
				scanf("%s",&courant->prenom);
				printf ("Entrez son adresse et le numero: (40) ");
				gets(x);
				//gets (courant->adressenum);
				scanf("%s",&courant->adressenum);
				printf ("Entrez son code postal et sa localite: (30) ");
				gets(x);
				//gets (courant->localitecp);
				scanf("%s",&courant->localitecp);
				printf ("Entrez son adresse email: (20)");
				gets(x);
				//gets (courant->email);
				scanf("%s",&courant->email);
 
				printf("\n");
                		printf("*|      Le contact %s %s bien enregistre     |*\n",courant->nom,courant->prenom);
				compteur++;
			break;
 
			case 3://Suppression d"un contact
                	printf("Supprimer un contact :");
			break;
 
			case 4://Rechercher un contact
			    printf("Rechercher un contact :");
			break;
 
		}
		choix=proposition();
	}
 
 
	if(compteur!=0)//rien a écrire si le répertoire est vide, soit compteur=0.
	{
   		courant=deb;
		courant=courant->suivant;
		while(courant->suivant!=NULL)
		{
			fprintf(fdat,"%-s\n",courant->nom);
			fprintf(fdat,"%-s\n",courant->prenom);
			fprintf(fdat,"%-s\n",courant->adressenum);
			fprintf(fdat,"%-s\n",courant->localitecp);
			fprintf(fdat,"%-s\n",courant->email);
			courant=courant->suivant;
		}
		fprintf(fdat,"%-s\n",courant->nom);
		fprintf(fdat,"%-s\n",courant->prenom);
		fprintf(fdat,"%-s\n",courant->adressenum);
		fprintf(fdat,"%-s\n",courant->localitecp);
		fprintf(fdat,"%-s\n",courant->email);
		courant=courant->suivant;
 
	}
 
	return (0); //arret propre du pgm.
}
 
void consultation(){
    FILE *fdat;
    fdat=fopen("projet.dat", "r");
    printf("   ________________________________________________  \n");
    printf("*| * *                          *           *      |*\n");
    printf("*| * * ***  **     *** *** **  ***  ** *** ***  ** |*\n");
    printf("*| * * * *  *      *   * * * *  *  * * *    *   *  |*\n");
    printf("*| * * *** **      *** *** * *  ** *** ***  ** **  |*\n");
    printf("*|  *                                              |*\n");
    printf("  \\_______________________________________________/\n");
}
 
int proposition(){
    int res;
	printf("   __________________________________________  \n");
	printf("*|               * *                         |*\n");
	printf("*|               *** *** **  * *             |*\n");
	printf("*|               *** **  * * * *             |*\n");
	printf("*|               * * *** * * ***             |*\n");
	printf("*|               * *                         |*\n");
	printf("*|-------------------------------------------|*\n");
	printf("*|                                           |*\n");
	printf("*| 1 ~  Consultation.                        |*\n");
	printf("*| 2 ~  Ajout d'un contact.                  |*\n");
	printf("*| 3 ~  Suppression d'un contact.            |*\n");
	printf("*| 4 ~  Rechercher un contact.               |*\n");
	printf("*| 0 ~  Quitter.                             |*\n");
	printf("*\\___________________________________________/*\n");
	printf("~  Entrez le numero correspondant votre choix:\n");
    scanf("%d",&res);
    //tant que l'ur ne rentre pas un nombre entre 1 et 5, il lui repose la question.
 
    while(res>5){
	    printf("Entrez un numero entre 1 et 5: ");
        scanf("%d",&res);
    }
    return res;
}