Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <stdio.h> #include <conio.h> #include <string.h> #include <stdlib.h> #include #include<string.h> #include<ctype.h> #define file "c:\\exo_malloc" struct etu { int mat; char nom[25]; int nbcotes; int *tab_cotes; }; int recherche_etu (void) { FILE *f; struct etu *p=NULL; char *matr=NULL; int mat=0; int i=0; int test=0; f=fopen(file,"rb"); printf(" recherche donnée d\'un étudiant \n"); printf(" ------------------------------------------\n"); printf(" entrez le matricule de l\'etudiant rechercher \n");fgets(matr,sizeof matr,stdin); mat=atoi(matr); if (f) { while(fread(p,sizeof *p,1,f)>0) { if (p->mat==mat) { printf(" Nom : %s a encoder %d\n\n",p->nom,p->nbcotes); for (i=0;inbcotes;i++) { printf(" cote n°%d : %d/25\n",i+1,p->tab_cotes[i]); } test=1; } getch(); } } // fin while else { printf(" erreur à l\'ouverture du fichier \n");getch(); } if (test==0) { printf(" Ce matricule n\'existe pas \n");getch(); } fclose(f); free(p); return 0; } int charger_fichier (void) { FILE*f; struct etu *p; f=fopen(file,"rb"); clrscr(); printf(" Liste des etudiants et leur matricule \n"); printf(" --------------------------------------- \n\n"); if (f) { while (fread(p,sizeof (struct etu),1,f)>0) { printf(" matricule -> psr%d nom -> %s \n",p->mat,p->nom); } getch(); } else { printf(" erreur à l\'ouverture du fichier \n"); getch(); } fclose(f); clrscr(); return 0; } int save ( struct etu *p) { FILE *f; f=fopen(file,"ab"); if (f) { fwrite(p,sizeof (struct etu),1,f); } clrscr(); printf(" etudiant Matricule \n\n"); printf(" %s psr%d \n",p->nom,p->mat); getch(); fclose(f); return 0; } int encode (void) { struct etu *p; FILE *f; int i=0; char *matri=NULL; char *cote=NULL; char *nbcote=NULL; char c=NULL; int tmp=0; clrscr(); f=fopen(file,"ab"); if(!f) { printf(" erreur à l\'ouverture \n"); getch(); } else { do { p=(struct etu *) malloc (sizeof(struct etu)); printf(" Entrez matricule entre 1 et 25 \n");gets(matri); p->mat=atoi(matri); printf(" Entrez nom de l\'etudiant :");gets(p->nom); printf(" entrez le nombres de cotes que vous voulez encoder : \n");gets(nbcote); p->nbcotes=atoi(nbcote); p->tab_cotes=(int*) malloc ((atoi(nbcote))*sizeof(int)); tmp=atoi(nbcote); for (i=0;i<tmp;i++) { printf(" cote n°%d -> \n",i+1);gets(cote); (*p).tab_cotes[i]=atoi(cote); } getch(); save(p); free(p); printf(" Voulez encoder un nouvel etudiant ? \n");c=toupper(getch()); } // fin while while (c=='O'); } // fin else fclose(f); return 0; } int main () { clrscr(); // remove(file); //encode(); recherche_etu(); charger_fichier (); return 0; }