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
|
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define nc 10000
//declaration enregistrement
struct client
{
short numclient;
char nom[25];
char prenom[25];
};
//protoype
void remplir(FILE *f);
void afficher(FILE *f);
//programme principal
int main()
{
struct client c;
char choix[3];
short n=0;
FILE *f;
printf("Bienvenue dans mon programme sur les manipulations des fichier\n");
printf("--------------------------------------------------------------");
do
{
printf("\nMenu\n");
printf("----\n");
printf("1.Remplir\n");
printf("2.Afficher\n");
printf("0.Quitter\n\n");
printf("Introduire votre choix:");
fgets(choix,sizeof(choix),stdin);
switch(choix[0])
{
case '1':remplir(f);
break;
case '2':afficher(f);
break;
case '0':;
break;
default:printf("Choix incorrect !!!\n");
}
}while(choix[0]!='0');
exit(0);
return 0;
}
void remplir(FILE *f)
{
struct client c;
if(f=fopen("tmp.dat","a+"))
{
printf("\nIntroduire le nom :");
fgets(c.nom,sizeof(c.nom),stdin);
printf("Introduire le prenom :");
fgets(c.prenom,sizeof(c.prenom),stdin);
c.numclient=nc;
fwrite(&c,sizeof(struct client),1,f);
}
fclose(f);
}
void afficher(FILE *f)
{
struct client c;
if(f=fopen("tmp.dat","r"))
{
if(feof(f))
printf("Fichier vide !!!\n");
else
{
while(!feof(f))
{
fread(&c,sizeof(struct client),1,f);
printf("Client %hd:\n",c.numclient);
printf("Nom : %s",c.nom);
printf("Prenom : %s\n",c.prenom);
}
}
fclose(f);
}
else
{
printf("Pas de fichier existant !!!\n");
}
} |
Partager