Bonjours j´ai un peit probleme avec fread en fait j´ai creer un programme ( une sorte d´agenda),donc j´écrit dans un fichier avec un fwrite les structur que se trouve dans u tableau,, il me le met dans mon fichier mais lorsaue je veux reprendres ces donnés qui sont dasn mon fichier pour les mettre dans mon tableau ça bug. voici mon code source:
- include < stdio.h>
- include < stdlib.h>
- include < string.h>
typedef struct individu{
char nom[31];
char prenom[31];
char adresse[61];
char ville[21];
char telephone[11];
int suivant;
}individu;
int Nb_fiche;
FILE * Fp_fiche;
void debuter_fiche(){
Fp_fiche=fopen("fiche.dat","a+");
if ( Fp_fiche == NULL) {
fprintf(stderr,"Erreur dans l´ouverture du fichier");
exit(-1);
}
fseek(Fp_fiche,0l,2);
Nb_fiche=ftell(Fp_fiche);
fseek(Fp_fiche,0l,2);
if(Nb_fiche==0)
fprintf(Fp_fiche,"%d\n",-1);
Nb_fiche=Nb_fiche/sizeof(individu);
fseek(Fp_fiche,0l,2);
}
void saisir_fiche(){
individu* tab_fiche;
tab_fiche=(individu*)malloc((Nb_fiche+10)*sizeof(i
ndividu));
if ( tab_fiche==NULL) {
printf("Erreur d´allocation mémoire ! !!");
exit(1);
}
printf("veuillez entrer un nom ( 31 charactere max)\n");
fgets(tab_fiche[Nb_fiche].nom,31,stdin);
printf("veuillez entrer un prenom ( 31 charactere max)\n");
fgets(tab_fiche[Nb_fiche].prenom,31,stdin);
printf("veuillez entrer une adresse ( 61 charactere max)\n");
fgets(tab_fiche[Nb_fiche].adresse,61,stdin);
printf("veuillez entrer une ville ( 21 charactere max)\n");
fgets(tab_fiche[Nb_fiche].ville,21,stdin);
printf("veuillez entrer un numero de telephone ( 11 charactere max)\n");
fgets(tab_fiche[Nb_fiche].telephone,11,stdin);
tab_fiche[Nb_fiche].suivant=Nb_fiche;
printf("Votre fiche a corectement été afficher\n");
fwrite(tab_fiche,Nb_fiche+1,sizeof(individu),Fp_fi
che);
Nb_fiche++;
fseek(Fp_fiche,0l,2);
}
void finir_fiche(){
fclose(Fp_fiche);
}
void lire_fiche(){
int i;
individu* tab_fiche;
tab_fiche=(individu*)malloc((Nb_fiche+10)*sizeof(i
ndividu));
if ( tab_fiche==NULL) {
printf("Erreur d´allocation mémoire ! !!");
exit(1);
}
fseek(Fp_fiche,0l,2);
fread(tab_fiche,Nb_fiche+1,sizeof(individu),Fp_fic
he);
printf("Votre agenda contient %d fiche(s) suivante(s)\n",Nb_fiche);
for(i=0;i<Nb_fiche;i++,tab_fiche++){
printf("%s\n",tab_fiche->nom);
printf("%s\n",tab_fiche->prenom);
printf("%s\n",tab_fiche->adresse);
printf("%s\n",tab_fiche->ville);
printf("%s\n",tab_fiche->telephone);
printf("%s\n",tab_fiche->suivant);
}
fseek(Fp_fiche,0l,2);
}
int main(){
debuter_fiche();
saisir_fiche();
/ ire_fiche();
finir_fiche();
printf("%d\n",Nb_fiche);
return 0;
}
voici ce qui se passe lors de l´execution du programme:
veuillez entrer un nom ( 31 charactere max)
peeroo
veuillez entrer un prenom ( 31 charactere max)
cedric
veuillez entrer une adresse ( 61 charactere max)
7766778n ryez
veuillez entrer une ville ( 21 charactere max)
paries
veuillez entrer un numero de telephone ( 11 charactere max)
987688768
Votre fiche a corectement été afficher
Votre agenda contient 1 fiche(s) suivante(s)
( null)
Est ce que j´utilise mal fread?
pour votre aide