Bonjour les gars, je suis débutant en C et j'ai un projet qui porte sur la gestion d'un annuaire téléphonique. Je bloque au niveau des fonctions affichage_alpha et supprimer. 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
 
#define true 0
#define false 1
 
 
struct personne
{
    char prenom[30];
    char nom[30];
    char localite[30];
    char tel[11];
 
};
typedef struct personne perso;
 
void saisir(perso *tab,int n,char nomfich[]);
void lister(perso *tab,int n);
void rechercher(perso *tab,int n,char num1[10]);
void rechercher1(perso *tab,int n,char local[20]);
void trialpha(perso *tab,int n);
void affichage_alpha();
void supprimer(perso *tab,int n,char num1[10],char nomfich[]);
 
int main()
{
    printf("BIENVENUE DANS LE PROGRAMME DE GESTION D'ANNUAIRE TELEPHONIQUE\n");
    int choix,n;
    char num1[10],name[20];
    char local[20];
    perso *tab;
    printf("donner le nombre de personne a ajouter\n");
    scanf("%d",&n);
    tab=(perso *)malloc(n* sizeof(perso));
 
do
{   printf("****************************\n");
    printf("1--> Ajouter des personnes\n");
    printf("2--> lister les personnes\n");
    printf("3--> rechercher une personne\n");
    printf("4--> rechercher une personne connaissant sa localite\n");
    printf("5--> Supprimer une personne\n");
    printf("6--> Lister les personnes par ordre alphabétique\n");
    printf("7--> quitter\n");
    printf("*****************************\n");
    printf("entrer votre choix\n");
    scanf("%d",&choix);
 
    switch(choix)
    {
        case 1:printf("donner le nom du nouveau fichier  \n");
               scanf("%s",name);
               printf("le nom du fichier est :\t%s\n",name);
          printf("Saisir les informations sur les personnes\n");
               saisir(tab,n,name);
               break;
        case 2: printf("Liste des personnes:\n");
                lister(tab,n);
                break;
        case 3: printf("recherche d 'une personne\n");
                printf("entrer le numero de telephone\n");
                scanf("%s",num1);
                rechercher(tab,n,num1);
                break;
        case 4: printf("recherche d 'une personne connaissant sa localite\n");
                printf("entrer la localite\n");
                scanf("%s",local);
                rechercher1(tab,n,local);
                break;
        case 5: printf("suppression d 'une personne\n");
                printf("entrer le numero de telephone\n");
                scanf("%s",num1);
                supprimer(tab,n,num1,name);
                break;
        case 5: printf("Liste par ordre alphabétique\n");
                affichage_alpha();
                break;
        default:printf("Merci et au revoir!\n");
    }
}
while(choix!=7);
free(tab);
return 0;
}
 
void saisir(perso *tab,int n,char nomfich[])
{   FILE *f;
    int i;
    f=fopen(nomfich,"w+");
    for(i=0;i<n;i++)
    {
        printf("entrer les informations de la personne %d\n",i+1);
        printf("donner son nom\n");
        scanf("%s",tab[i].nom);
        printf("entrer son prenom\n");
        scanf("%s",tab[i].prenom);
        printf("entrer sa localite\n");
        scanf("%s",tab[i].localite);
        printf("entrer le numero de telephone\n");
        scanf("%s",tab[i].tel);
    }
        for(i=0;i<n;i++)
        {
        fprintf(f, "nom:%s\nprenom:%s\nlocalite:%s\ntelephone:%s\n", tab[i].nom,tab[i].prenom,tab[i].localite,tab[i].tel);
        fclose(f);
    }
}
 
void lister(perso *tab,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("Personne %d\n",i+1);
        printf("Nom:%s\n",tab[i].nom);
        printf("Prenom:%s\n",tab[i].prenom);
        printf("Localite:%s\n",tab[i].localite);
        printf("Tel:%s\n",tab[i].tel);
    }
}
void rechercher(perso *tab,int n,char num1[10])
{
 int i,trouve,p;
 char trouveprenom[20],trouvenumero[10],trouvenom[20];
 trouve=false;
 i=0;
 while((i<n)&&(trouve==false))
 {
     p=strcmp(num1,tab[i].tel);
     if(p==0)
     {
        strcpy(trouveprenom,tab[i].prenom);
        strcpy(trouvenom,tab[i].nom);
        strcpy(trouvenumero,tab[i].tel);
        trouve=true;
     }
     else
     i++;
 }
 if(trouve==true)
     {
        printf("Nom:%s\n",tab[i].nom);
        printf("Prenom:%s\n",tab[i].prenom);
        printf("Localite:%s\n",tab[i].localite);
        printf("Telephone:%s\n",tab[i].tel);
 
     }
 else
 printf("ce nom n existe pas dans l'annuaire\n");
 }
void rechercher1(perso *tab,int n,char local[20])
{
 int i,trouve,p;
 char trouveprenom[20],trouvenom[20],trouvetel[20];
 trouve=false;
 i=0;
 while((i<n)&&(trouve==false))
 {
     p=strcmp(local,tab[i].localite);
     if(p==0)
     {
        strcpy(trouveprenom,tab[i].prenom);
        strcpy(trouvenom,tab[i].nom);
        strcpy(trouvetel,tab[i].tel);
        trouve=true;
     }
     else
     i++;
 }
 if(trouve==true)
 {
        printf("Nom:%s\n",trouvenom);
        printf("Prenom:%s\n",trouveprenom);
        printf("Telephone:%s\n",trouvetel);
 }
 else
 printf("ce numero n existe pas dans l'annuaire\n");
 }
 
 /****************************TRI ALPHABETIQUE DES NOMS*****************************/
 
void trialpha(perso *tab,int n)
{
 
     int i,j;
     char temp[32];
 
       for (i=0;i<n;i++)
       {
           for (j=0;j<n;j++)
           {
               if (strcmp(tab[i].nom,tab[j].nom)<0)
               {
 
                   strcpy(temp,tab[i].nom);
                   strcpy(tab[i].nom,tab[j].nom);
                   strcpy(tab[j].nom,temp);
                   fflush(stdin);
                   strcpy(temp,tab[i].prenom);
                   strcpy(tab[i].prenom,tab[j].prenom);
                   strcpy(tab[j].prenom,temp);
                   fflush(stdin);
                   strcpy(temp,tab[i].localite);
                   strcpy(tab[i].localite,tab[j].localite);
                   strcpy(tab[j].localite,temp);
                   fflush(stdin);
                   strcpy(temp,tab[i].tel);
                   strcpy(tab[i].tel,tab[j].tel);
                   strcpy(tab[j].tel,temp);
                   fflush(stdin);
               }
               else
                    if(strcmp(tab[i].nom,tab[j].nom)==0)
                    {
                          if (strcmp(tab[i].prenom,tab[j].prenom)<0)
                          {
 
                            strcpy(temp,tab[i].nom);
                            strcpy(tab[i].nom,tab[j].nom);
                            strcpy(tab[j].nom,temp);
                            fflush(stdin);
                            strcpy(temp,tab[i].prenom);
                            strcpy(tab[i].prenom,tab[j].prenom);
                            strcpy(tab[j].prenom,temp);
                            fflush(stdin);
                            strcpy(temp,tab[i].localite);
                            strcpy(tab[i].localite,tab[j].localite);
                            strcpy(tab[j].localite,temp);
                            fflush(stdin);
                            strcpy(temp,tab[i].tel);
                            strcpy(tab[i].tel,tab[j].tel);
                            strcpy(tab[j].tel,temp);
                            fflush(stdin);
                          }
                     }
 
           }
       }
 
}
 
/***********************AFFICHAGE TRIE PAR NOM***********************************/
 
void affichage_alpha()
{
 
     trialpha(tab,n);
     printf("\n--------------------AFFICHAGE DES ENREGISTREMENTS------------------------\n");
     if (n!=0)
     {
         for (i=0;i<n;i++)
         {
             printf("\nEnregistrement(s) NUMERO : %d \n",i+1);
             printf("NOM : %s \n",tab[i].nom);
		     printf("PRENOM : %s \n",tab[i].prenom);
             printf("ADRESSE : %s \n",tab[i].localite);
             printf("TELEPHONE : %s \n",tab[i].tel);
         }
      }
      else printf("Aucun enregistrement en memoire\n");
}
 
 
void supprimer(perso *tab,int n,char num1[10],char nomfich[])
{
    int i;
  FILE *fichier, *sortie;
  char enleve[32];
  do
    {
     fichier = fopen("annuaire.txt", "r");
     if (!fichier)
         printf("ERREUR: Impossible d'ouvrir le fichier: \n");
    }
  while (!fichier);
  do
    {
 
     sortie = fopen("suppression.txt", "w");
     if (!sortie)
         printf("ERREUR: Impossible d'ouvrir fichier: \n");
    }
  while (!sortie);
  /* Saisie de l'enregistrement à supprimer */
  printf("Enregistrement à supprimer : ");
  scanf("%s",enleve);
  /* Traitement */
  /* Copie de tous les enregistrements à */
  /* l'exception de celui à supprimer.   */
  i=0;
  while (!feof(fichier))
    {
 
     fscanf(fichier, "%s\n", tab[i].nom);
 
     if (strcmp(tab[i].nom, enleve) != 0)
     {
          fprintf(sortie, "%s\n", tab[i].nom);
          fprintf(sortie, "%s\n", tab[i].prenom);
          fprintf(sortie, "%s\n", tab[i].localite);
          fprintf(sortie, "%s\n", tab[i].tel);
     }
     i++;
    }
  /* Fermeture des fichiers */
  fclose(sortie);
  fclose(fichier);
//  key=getch();
 
}
Merci pour votre aide.