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
|
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include "menu.h" // header file gestion du menu
#define file "c:\\exo"
#define file1"c:\\exo2"
#define MAXETU 25
struct etu
{ int mat;
char *nom;
int *tab_cote;
int nbcotes;
} ;
/* ********************************************************************* */
void save( struct etu *tab)// en parametre-> le nouvel enregistrement à sauvé
{
FILE *f=fopen(file,"ab");// ouvre en append donc ajoute sans écraser
if (f)
{ fwrite(tab,sizeof (struct etu),1,f);
}
else
{ printf(" erreur à l\'ouverture du fichier \n"); }
fclose(f);
}
/* ************************************************************* */
void init_tab (struct etu *tab)
{ int i=0;
tab->mat=0;
tab->nom=NULL;
}
/* ********************************************************************* */
int encode ()
{
struct etu *p,*tab;
int nb=0;
char *matri=NULL;
int i=0;
int lg=0;
char nom[25];
char *nbcote;
int ncote=0;
char *nb1=NULL;
int compteur=0;
int tmp=0;
char c;
FILE *f;
do
{
p=(struct etu *) malloc(sizeof(struct etu));
printf(" entrez votre nom\n") ;fflush(stdin);gets(p->nom);
printf(" liste des matricules deja utilisé [entre 1 et 25] ! : \n\n");
f=fopen(file,"rb");
fread(tab,sizeof (struct etu),1,f);
while (!feof(f))
{
printf(" -> %i \n",tab->mat);
fread(tab,sizeof (struct etu),1,f);// lit bloc par bloc
}// fin while
fclose(f);
i=0;
printf(" choisir un matricule entre 1 et 25 qui n'est pas dans la liste\n");fflush(stdin);
gets(matri);
p->mat=atoi(matri);
clrscr();
printf(" entrez le nombre de cote que vous voulez encoder : \n");fflush(stdin);gets(nbcote);
ncote=atoi(nbcote);
p->nbcotes=ncote;
p->tab_cote=(int*) malloc(ncote*sizeof(int));
for (i=0;i<ncote;i++)
{ printf(" cote n°%d -> ",i+1);gets(nb1);
p->tab_cote[i]=atoi(nb1);
nb1=NULL;
}
printf(" Monsieur/Madame %s avez comme matricule=> psr00%i\n\n",p->nom,p->mat);
printf(" vos cotes sont les suivantes : \n\n");
for (i=0;i<ncote;i++)
{
printf("cote %d -> %d\n",i+1,p->tab_cote[i]);
}
getch();
save(p); // sauve l'enregistrement dans le fichier binaire
printf(" \n\n\n voulez vous encoder un nouvel etudiant ? \n");
c=toupper(getch());
clrscr();
free(p);
}
while (c=='O');
clrscr();
free(tab);
return 0;
}
/* *********************************************************************** */
int charger_fichier ()
{
int i=0;
struct etu *p;
FILE *f=fopen(file,"rb");
clrscr();
if (f)
{ fread(p,sizeof (struct etu),1,f);
while(!feof(f))
{
fread(p,sizeof (struct etu),1,f);
printf(" Mr/Mme %s matricule n°%d\n",p->nom,p->mat);
for (i=0;i<p->nbcotes;i++)
printf(" cote n°%d -> %i\n",i+1,p->tab_cote[i]);
}
getch();
}
fclose(f);
free(p);
return 0;
}
/* ******************************************** */
main ()
{ clrscr();
/* if(remove(file)==0) {printf(" suppression effectué avec succes \n");}
getch();
*/
encode();
// gestion();// fonction du menu.h
charger_fichier();
printf("pressez une touche ...\n");
getch();
return 0;
} |
Partager