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
| #include<stdio.h>
#include<conio.h>
#include<string.h>
FILE *f;
typedef struct
{
char nom[20];
int mat;
}etud;
etud tab[100],e;
etud tmp;
int main()
{
int l,i,j;
char rep;
etud e;
f=fopen("etudiant.dat","w");
do
{
printf("Pour l'étudiant n° %d",i+1);
printf("taper le nom :\n");
scanf("%s",e.nom);
printf("taper le matricule :\n");
scanf("%d",&e.mat);
fwrite(&e,sizeof(etud),1,f);
printf("voulez vous tapper un autre etudiant ");
scanf(" %c",&rep);
}while(rep=='O'||rep=='o');
fclose(f);
f=fopen("etudiant.dat","r+");
fseek(f,0,2);
l=ftell(f)/sizeof(etud);
fread(tab,l*sizeof(etud),1,f);
/*faire le tri*/
for(i=0;i<l;i++)
for(j=i+1;j<l-1;j++)
if(strcpy(tab[i].nom,tab[j].nom)>0)
{
tab[i]=tmp;
tmp=tab[j];
tab[j]=tab[i];
}
fwrite(tab,sizeof(etud),l,f);
fclose(f);
f=fopen("etudiant.dat","r");
do
{
fread(&e,sizeof(etud),1,f);
if(!feof(f))
printf("%s %d \n",e.nom,e.mat);
}while(!feof(f));
fclose(f);
getch();
return 0;
} |
Partager