La memoire ne peut etre en etat written / read

Fermé
hammazit Messages postés 1 Date d'inscription mardi 1 mai 2018 Statut Membre Dernière intervention 1 mai 2018 - Modifié le 1 mai 2018 à 14:54
[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 - 2 mai 2018 à 16:24
well i i need a help to resolve this problem ! that's my code !! please helppp
#include<stdio.h>
#include <stdlib.h>
FILE* frdv ;
FILE* fmedic; 
FILE* fpa ;
FILE* test ;
typedef struct {
        int j ;
        int m ;
        int an ;
        }date ;

typedef struct patient {
        int num ;
        char nom[20];
        char prenom[20] ;
        char adr[30];
        date date_nai ;
        char lieu[15];
        char etat[10] ;
        }patient ;

typedef struct medic {
        int code ;
        char nom[15];
        int qua ;
        }medic ;

typedef struct rdv {
  int code ;
  date date_rdv ;
        }rdv ;

typedef patient *listpatient;  
typedef medic *listmedic ;
typedef rdv *listrdv ; 

listpatient ajouter_p ( listpatient tmp) 
 {
 
 printf("donner le numero du patient \n") ; 
 scanf("%d",&tmp->num) ; 
 printf("donner le nom du patient \n ") ; 
 scanf("%s",tmp->nom) ; 
 printf("donner le prenom du patient \n") ; 
 scanf("%s",tmp->prenom) ; 
 printf("donner l'adresse du patient \n'") ; 
 scanf("%s",tmp->adr) ; 
 printf("donner date naissance du patient JJ/MM/AN \n") ; 
 scanf("%d %d %d", &tmp->date_nai.j,&tmp->date_nai.m,&tmp->date_nai.an) ; 
 printf("donner lieu de naissance du patient \n") ; 
 scanf("%s",tmp->lieu) ; 
 printf("donner l'etat du patient \n'") ; 
 scanf("%s",tmp->etat) ;  


return(tmp) ;
}

void ajouter_patient()
{listpatient p; char v='o'; 

 fpa=fopen("patient.txt","a+");
 do{
 ajouter_p(p);
 fwrite(&p,sizeof(listpatient),1,fpa);
 printf("Ajouter un nouveaux patient: (O/N): \n ");
 scanf("%s", &v);
 }while(v=='o');
   
}

void supprimer_p()
{listpatient tmp=NULL,tmpp=NULL; int nm;
   
 test=fopen("test.txt","w+");
 fpa=fopen("patient.txt","r+");
 printf("Donner le numero de patient a supprimer: \n");
 scanf("%d",&nm);
 while(fread(&tmp,sizeof(listpatient),1,fpa))
 {
  if (tmp->num!=nm)
   fwrite(&tmp,sizeof(listpatient),1,test);
 };
 fclose(fpa);
 fclose(test);
 test=fopen("test.txt","rb");
 fpa=fopen("patient.txt","wb+");
 while(fread(&tmpp,sizeof(listpatient),1,test))
 {
  fwrite(&tmpp,sizeof(patient),1,fpa);
 };
 fclose(fpa);
 fclose(test);
}

void afficher_p(listpatient p)
{
 printf("Num patient %d",p->num) ;
 printf("Nom patient %s",p->nom) ; 
 printf("Prenom patient s",p->prenom) ;
 } 
 
void modifier_p(listpatient p)
{ listpatient tmp=p;int num;
   
 test=fopen("test.txt","wb+");
 fpa=fopen("patient.txt","rb+");
 printf("Le numero de patient a modifier: \n");
 scanf("%d",&num);
 while(fread(&tmp,sizeof(listpatient),1,fpa)){
  if (tmp->num==num)
   ajouter_p(tmp);
   fwrite(&tmp,sizeof(patient),1,test);
 }
 fclose(fpa);
 fclose(test);
 test=fopen("test.txt","rb");
 fpa=fopen("patient.txt","wb+");
 while(fread(&tmp,sizeof(patient),1,test)){
  fwrite(&tmp,sizeof(patient),1,fpa);
 }
 fclose(fpa);
 fclose(test);
}
 
void chercher_p(int nm)
{listpatient p ;
 fpa=fopen("patient.txt","rb");
 while(fread(&p,sizeof(patient),1,fpa))
 {
  if(p->num == nm)
   afficher_p(p);
 }
}

listrdv ajout_rdv(listrdv r)
{
 listrdv tmp=r; 
 printf("donner le numero du rendez-vous: ");
 scanf("%d",tmp->code); 
 printf("entrer la date de rendez-vous Jour/Mois/Annee:\n ");
 printf("Jour:\n");
 scanf("%d",tmp->date_rdv.j);
 printf("Mois:\n");
 scanf("%d",tmp->date_rdv.m);
 printf("Annee:\n");
 scanf("%d",tmp->date_rdv.an);
 return (tmp);
}
  
void ajouter_r(listrdv r){
 listrdv tmp=r ;
 char verif='o'; 
 frdv=fopen("rendezvous.txt","a+");
 do{
  ajout_rdv(tmp);
  fwrite(&tmp,sizeof(tmp),1,frdv);
  printf("Voulez vous ajouter un nouveau RDV(O/N) \n ");
  scanf("%d", &verif);
  }
 while(verif =='o');
 fclose(frdv);
   
  }



listrdv modif_r(listrdv r){
 listrdv tmp=r; listrdv rd=NULL ; listrdv rdvv=NULL ;
 int num;
 test=fopen("testf.txt","w+");
 frdv=fopen("RDV.txt","r+");
 printf("Donner le numero de patient ");
 scanf("%d",&num);
 while(fread(&tmp,sizeof(tmp),1,frdv)){
  if (tmp->code==num)
   ajout_rdv(rd);
   fwrite(&rd,sizeof(listrdv),1,test);
  }
 fclose(test);
 test=fopen("testf.txt","r");
 frdv=fopen("RDV.txt","w+");
 while(fread(&rdvv,sizeof(listrdv),1,test)){
 fwrite(&rdvv,sizeof(listrdv),1,frdv);
}
 fclose(frdv);
 fclose(test);
 }

void afficher_rendervous(listrdv r)
{ listrdv tmp=r ;
 printf("Code du RDV : %d \n",tmp->code);
 printf("Date est:%d/%d/%d\n",tmp->date_rdv.j,tmp->date_rdv.m,tmp->date_rdv.an);
   
}
   
void chercher_r(int num){
 frdv=fopen("RDV.txt","r");
 listrdv p;
 while(fread(&p,sizeof(listrdv),1,frdv)){
  if(p->code == num)
   afficher_rendervous(p);
  }
 fclose(frdv);
 }


listmedic saisie_m(listmedic tmp)
{   
 printf("Le code du MEDIC:\n ");
 scanf("%d",tmp->code); 
 printf("Le nom du MEDIC: \n ");
 scanf("%s",tmp->nom);
 printf("La quantité du MEDIC: \n  ");
 scanf("%d",tmp->qua);
return (tmp);
}
  
void afficher_m(listmedic m)
{ listmedic tmp=m ;
 printf("Code :%d\n",tmp->code);
 printf("Nom:%s\n",tmp->nom);
 printf("Quantité :%d\n",tmp->qua);
}
   

void ajouter_m()
{ listmedic tmp=NULL; char v='o'; 
 
 fmedic=fopen("medic.txt","a+");
 do{
 saisie_m(tmp);
 fwrite(&tmp,sizeof(listmedic),1,fmedic);
 printf("Voulez vous ajouter un nouveau medicament: (O/N): \n ");
 scanf("%s", &v);
 }while(v == 'o');
   
}
  
  
void supprimer_m(listmedic m)
{  listmedic tmp=NULL,tmpp=NULL; int num;
   
 test=fopen("test.txt","w+");
 fmedic=fopen("medic.txt","r+");
 printf("Donner code du MEDIC a supprimer: \n");
 scanf("%d",&num);
 while(fread(&tmp,sizeof(listmedic),1,fmedic))
 {
  if (tmp->code!= num)
   fwrite(&tmp,sizeof(listmedic),1,test);
 }
 fclose(fmedic);
 fclose(test);
 test=fopen("test.txt","rb");
 fmedic=fopen("medic.txt","wb+");
 while(fread(&tmpp,sizeof(listmedic),1,test))
 {
  fwrite(&tmpp,sizeof(listmedic),1,fmedic);
 }
 fclose(fmedic);
 fclose(test);
}
   
void chercher_m(int num)
{ listmedic tmp=NULL;
 fmedic=fopen("medicament.txt","rb");
 
 while(fread(&tmp,sizeof(listmedic),1,fmedic))
 {
 if(tmp->code == num)
  afficher_m(tmp);
 }
fclose(fmedic) ;  
}






int main()
{
listpatient p=NULL ; listrdv rdv=NULL ; listpatient pa=NULL ; listmedic m=NULL ;
int rep,choi,choix,i,num;

do{

  printf("***************Bienvenue sur notre cabinet***************\nVeuiller saisir:\n(1)Gestion des patients\n(2)Gestion des rendez-vous\n(3)Gestion des medicaments \n");
  scanf("%d",&rep);
  switch (rep){
   case 1: printf("(1)Ajouter un patient \n (2)Supprimer un patient \n (3)Chercher un patient \n (4)Modifier un patient \n ") ;
      scanf("%d",choi) ;
     switch (choi) {
           case 1:ajouter_p(p) ; 
       case 2:supprimer_p(p) ; 
       case 3:chercher_p(i) ;
       case 4:modifier_p(p) ;
          }     
   break;
   
   case 2: printf("(1)Ajouter un Rendez-Vous \n (2)Modifier un Rendez-vous \n ") ;
        scanf("%d",&choix) ;
        switch(choix){
             case 1:ajouter_r(rdv) ; 
             case 2:modif_r(rdv) ;
             case 3:chercher_r(i);
               }
   break;
   case 3: printf("(1)Ajouter un medicament \n (2)Supprimer un medicament \n (3)Chercher un medicament \n") ;
        scanf("%d",&num) ; 
        switch(num){
             case 1: ajouter_m();
         case 2: supprimer_m(m);
       case 3: chercher_m(i);
       }
   break;
   }
    }while (rep!=3);
return 0; 
}

1 réponse

[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 1 083
Modifié le 2 mai 2018 à 16:27
Salut hammazit,

La compilation de ton programme avec gcc et les warnings montre qu'il produit de nombreux avertissements :

$ gcc -Wall 35342194.c
35342194.c: In function ‘afficher_p’:
35342194.c:101:2: warning: too many arguments for format [-Wformat-extra-args]
printf("Prenom patient s",p->prenom) ;
^
35342194.c: In function ‘ajout_rdv’:
35342194.c:141:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",tmp->code);
^
35342194.c:144:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",tmp->date_rdv.j);
^
35342194.c:146:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",tmp->date_rdv.m);
^
35342194.c:148:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",tmp->date_rdv.an);
^
35342194.c: In function ‘ajouter_r’:
35342194.c:160:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
scanf("%d", &verif);
^
35342194.c: In function ‘saisie_m’:
35342194.c:212:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",tmp->code);
^
35342194.c:216:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",tmp->qua);
^
35342194.c: In function ‘main’:
35342194.c:294:6: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",choi) ;
^
35342194.c:285:54: warning: unused variable ‘pa’ [-Wunused-variable]
listpatient p=NULL ; listrdv rdv=NULL ; listpatient pa=NULL ; listmedic m=NULL ;
^
35342194.c: In function ‘modif_r’:
35342194.c:189:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
35342194.c: In function ‘main’:
35342194.c:294:6: warning: ‘choi’ may be used uninitialized in this function [-Wmaybe-uninitialized]
scanf("%d",choi) ;
^
35342194.c:298:14: warning: ‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
case 3:chercher_p(i) ;

Les avertissements n'empêchent pas la compilation, mais te préviennent en général d'un comportement dangereux, inadéquat ou non recommandé à rectifier à moins que tu saches très exactement ce que tu fais. Ils révèlent des bogues dans 90% des cas.

Tu ne dois pas ignorer les avertissements, mais les traiter un par un, comprendre pourquoi ils se manifestent et rectifier ton code afin que ton code compile proprement, sans warnings. Commence par le premier. Après avoir corrigé un avertissement, recompile, et passe au suivant (d'autres apparaîtront éventuellement).

Une des erreurs récurrentes que tu fais est que, parfois, tu ne passes pas à scanf un pointeur, comme tu le devrais.

Par exemple, en ligne 322,
scanf("%d",choi);
provoque une erreur de segmentation, comme "choi" est déclaré en tant que
int choi;
, alors que tu devrais passer l'adresse de "choi" à scanf, comme cela :
scanf("%d",&choi);
... tu envoies à scanf le contenu de "choi" (qui de plus n'est pas initialisé) au lieu d'envoyer l'adresse mémoire de "choi".

Ce type d'erreur est signalé par les warnings à de nombreux endroits de ton code.

Ainsi, pour
scanf("%d",tmp->code);
en ligne 141, qui devrait être
scanf("%d",&tmp->code);
...

Corrige ton code et vois si cela fait disparaître tes erreurs mémoire.


Dal
0