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
| #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
/*programme principal */
main()
{
FILE *fp , *ft;
char autrechoix, choix , nomfichier[30];
int i ;
/*structure de l'employe*/
struct emp{
int matricule;
char nom[40];
char prenom[40];
char sexe [1];
int datenai ;
};
/*structure */
struct emp e;
/*variable pour enregistrer le matricule des employes*/
char empmatricule[40];
/*taille de chaque enregistrement */
long int recsize;
/*creation du fichier */
fp = fopen("EMP.DAT","rb+");
if(fp == NULL)
{
fp = fopen("EMP.DAT","wb+");
if(fp == NULL)
{
printf("Impossible d'ouvrir le fichier");
exit(1);
}
}
/*taille de chaque enregistrement exemple pour la structure e */
recsize = sizeof(e);
/*creer le fichier a remplir*/
fseek(fp,0,SEEK_END);
for (i=0;i<4;i++)
{
printf("----employe #%d--------- \n",i+1);
printf("entrez le numero de matricule SVP\n");
scanf("%d",&e.matricule);
printf("entrez le nom \n");
scanf("%s",e.nom);
printf("entrez le prenom\n");
scanf("%s",e.prenom);
printf("sexe :M/F\n");
scanf("%s",e.sexe);
printf("votre date de naissance (JJ/MM/AAAA)\n");
scanf("%d",&e.datenai);
fwrite(&e,recsize,1,fp);
}
printf("|-----Bienvenue au Menu de Gestion---------------------|\n");
printf("|1-ajouter un employe a la liste |\n");
printf("|2-affichage de tous les enregistrements de ce fichier |\n");
printf("|3-modification des informations d'un employe' |\n");
printf("|4-consultation a partir du matricule |\n");
printf("|5-supprimer une info grace a son matricule |\n");
printf("|------------------------------------------------------|\n");
fflush(stdin);
choix = getche();
switch(choix)
{
case '1':
autrechoix = 'y';
fseek(fp,0,SEEK_END);
while(autrechoix == 'y')
{
printf("----nouveau employe--------- \n");
printf("entrez le numero de matricule SVP\n");
scanf("%d",&e.matricule);
printf("entrez le nom \n");
scanf("%s",e.nom);
printf("entrez le prenom\n");
scanf("%s",e.prenom);
printf("sexe :M/F\n");
scanf("%s",e.sexe);
printf("votre date de naissance (JJ/MM/AAAA)\n");
scanf("%d",&e.datenai);
fwrite(&e,recsize,1,fp);
printf("\n un autre enregistrement ? (y/n) ");
fflush(stdin);
autrechoix = getche();
}
break;
case '2':
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
printf("\n %d %s %s %s %d",e.matricule,e.nom,e.prenom,e.sexe,e.datenai);
}
getch();
break;
case '3':
autrechoix = 'y';
while(autrechoix == 'y')
{
printf("Entrer le numero de matricule de l'employe:");
scanf("%s", empmatricule);
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{ /*comparaison des donnees */
if(strcmp(e.nom,empmatricule) == 0)
{ /* si le matricle correspond */
printf("\n re-entrer a nouveau les information : ");
scanf("%d %s %s %s %d",&e.matricule,e.nom,e.prenom,e.sexe,&e.datenai);
fseek(fp,-recsize,SEEK_CUR); /*changer la position du curseur */
fwrite(&e,recsize,1,fp); /*ecraser l'enregistrement*/
break;
}
}
printf("\n modifier d'autres donnees ? (y/n)");
fflush(stdin);
autrechoix = getche();
}
break;
case '4':
autrechoix = 'y';
while(autrechoix == 'y')
{
printf("\nEntrez le numero de matricule de l'employe a supprime: ");
scanf("%s",empmatricule);
/*creation d'un fichier temporaire*/
ft = fopen("tmp","wb");
rewind(fp);
while(fread(&e,recsize,1,fp) == 1)
{
/*lire tous les enregistrements du fichier*/
if(strcmp(e.nom,empmatricule) != 0)
{
/* si les donnees entrees correspondent*/
/*deplacer tous les enregistrements excpetes ce qui doit etre efface */
fwrite(&e,recsize,1,ft);
printf("supprimer un autre enregistrement ? (y/n)");
fflush(stdin);
autrechoix = getche();
}
}
}
fclose(fp);
fclose(ft);
remove("EMP.DAT"); /*retirer le fichier original*/
rename("tmp.dat","EMP.DAT"); /*renomer le fichier temporaire au fichier original*/
fp = fopen("EMP.DAT", "rb+");
break;
}
return 0;
} |
Partager