Bonjour

Dans ce programme on est censé saisir un nom et un numero de tel. qu'on enregistre dans un fichier.

Ensuite, on recherche un mot et si le mot existe dans le fichier on l'affiche avec le numero correspondant.

Mon problème:

Tant que je m'en servais d'un fichier texte pour enregistrer les donneés, je n'avais aucun problème d'affichage à l'écran.

Mais lorsque j'essaies d'enregistrer les données sous forme de bloc...jles données lues ne s'affichent pas à l'écran.

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
struct Enreg{
       char nom[40];
       int tel[10];
       };
 
 
 
int menu(void);
void creer(struct Enreg fiche);
void recherche(struct Enreg fiche);
void lecture_struct(struct Enreg fiche);
void lecture_mot(char mot[]);
 
 
FILE *fd;
struct Enreg fiche;
char mot[40]={0};
int main()
 
{
    int choix;
 
 
 
    do{
        choix=menu();
        switch(choix){
                      case 1:
                           //dbt=(Nd*)malloc(sizeof(Nd));
                           creer(fiche);
                           printf("\n");
 
                           continue;
 
                       case 2:
                           recherche(fiche);
 
                           printf("\n");
 
                           continue;
 
 
                      default:
                              printf("Au revoir...\n\n");
                              //break;
                              }
                             } while(choix!=3);
 
           return 0;
 
}
 
int menu(void)
{
    int i;
    do{
        printf("\nMenu Principal\n");
               printf(" 1 - Creation\n");
               printf(" 2 - Ajout\n");
               printf(" 3 - Fin\n");
               printf("Indiquez l'op choisie(1. 2. 3.): ");
               scanf("%d", &i);
 
               if(i<1 || i>3)
               printf("\nErr recommencez\n");
               }while(i<1 || i>3);
               printf("\n");
               return(i);
               }
void creer(struct Enreg fiche)
{
        int rep=0;
 
        printf("Sous-Programme création dune fiche.\n");
        printf("Appuyez sur une touche...\n");
        getch();
 
 
 
            fflush(stdin);
            lecture_struct(fiche);
 
 
             printf("Voulez vous conserveez cette fiche(1=yes//0=no): ");
             scanf("%d", &rep);
 
             if(rep==1){
 
             fd=fopen("repertoire.bin", "a+b");
             if(fd==NULL){
                     printf("\nError lors de l/'ouverture du fichier spécifié.!\n");
                     }
                     else{
 
                          fseek(fd, 0, SEEK_END);
                          fwrite(&fiche, sizeof(struct Enreg), 1, fd);
 
                          fclose(fd);
                          printf("Sauvegarde reussie!\n");
                          }
                         fclose(fd);
                     }
 
                            else{
                                printf("Echec sauvegarde!\n");
                                }
 
             return;
        }
 
 
 
void recherche(struct Enreg fiche)
{
    int rep;
    printf("Sous-Programme recherche dune fiche.\n");
    printf("Appuyez sur une touche...\n");
    getch();
 
        lecture_mot(mot);
        fd=fopen("repertoire.bin", "rb");
        if(fd==NULL){
                     printf("Echec");
                     }
                     else{
                         fseek(fd, 0, SEEK_SET);
                         while(!feof(fd))
                         {
                            fread(&fiche, sizeof(struct Enreg), 1, fd);
                            //fscanf(fd, "%s\n", fiche.nom);
                            if(strcmp(fiche.nom, mot)==0)
                            {
                                printf("%s ", fiche.nom);
                                printf("\n%d", fiche.tel);
                                printf("Continuer la rechercher?(0=oui/1=non): ");
                                scanf("%d", &rep);
                                if(rep!=0) break;
                            }
 
                             if(feof(fd))
                             {
                                printf("\n\nFin du fichier\nAppuyez sur une touche...\n");
                                getch();
                             }
                        }
                fclose(fd);
                            }
        }
 
 
/* --- Saisie members of struct ---*/
void lecture_struct(struct Enreg fiche)
{
  char c;
    int i=0;
 
/* --- saisie du nom ---*/
 
    printf("nom :");
	fflush(stdin);
 
    do{
        c=getchar();
        fiche.nom[i]=c;
        i++;
    }while(c!='\n');
 
    fiche.nom[i-1]='\0';
 
/* --- saisie tel number ---*/
 
 printf("tel:");
 scanf("%d", &fiche.tel);
 
 
 
 
return;
}
 
/* --- Saisie word to search ---*/
void lecture_mot(char mot[])
{
  char c;
    int i=0;
 
 
    printf("mot :");
	fflush(stdin);
 
    do{
        c=getchar();
        mot[i]=c;
        i++;
    }while(c!='\n');
 
    mot[i-1]='\0';
 
return;
}
voila