mardi 13 décembre 2011

programme de gestion de fichier

 Créer une structure nom, prénom, ‚ge. Écrire un programme de gestion de fichier (texte) avec menu d'accueil: possibilité de créer le fichier, de le lire, d'y ajouter une fiche, d'en rechercher une.

#include <stdio.h>
#include <conio.h>
#include <string.h>
typedef struct
{
char nom[10];
char prenom[10];
int age;
}
carte;/* creation d'un type carte */
void creer_fichier(FILE *f,char *n)
{
char choix;
carte fiche;
clrscr();
printf("CREATION DU FICHIER \n\n");
printf("NOM DU FICHIER A CREER: ");
gets(n);
flushall();
f = fopen(n,"w");
do
{
printf("\nSAISIE D'UNE FICHE ?(o/n) ");
choix = (char)getchar();
   flushall();
if ((choix=='o')||(choix=='O'))
{
printf("\nNOM: ");gets(fiche.nom);
printf("PRENOM: ");gets(fiche.prenom);
printf("AGE: ");scanf("%d",&fiche.age);
flushall();
fwrite(&fiche,sizeof(carte),1,f);
}
}
while((choix=='o')||(choix=='O'));
fclose(f);
}
void lire_fichier(FILE *f,char *n)
{
carte fiche;
int compteur=0;
clrscr();
printf("LECTURE DU FICHIER\n\n");
printf("NOM DU FICHIER A LIRE: ");gets(n);
flushall();
f = fopen(n,"r");
if (f == NULL) printf("\nERREUR, CE FICHIER N'EXISTE PAS\n\n");
else
{
printf("\nLISTING DU FICHIER\n\n");
while(fread(&fiche,sizeof(carte),1,f)!=0)
{
printf("fiche nø%d: \n",compteur);
compteur++;
printf("%s %s %d an(s)\n\n",fiche.nom,fiche.prenom,fiche.age);
}
fclose(f);
}
printf("POUR CONTINUER FRAPPER UNE TOUCHE ");
getch();
}
void ajout(FILE *f,char *n)
{
carte fiche;
char choix;
clrscr();
printf("AJOUT D'UNE FICHE \n\n");
printf("NOM DU FICHIER A MODIFIER: ");
gets(n);
flushall();
f = fopen(n,"a");
do
{
printf("\nSAISIE D'UNE FICHE ?(o/n) ");
choix = (char)getchar();
   flushall();
if ((choix=='o')||(choix=='O'))
{
printf("\nNOM: ");gets(fiche.nom);
printf("PRENOM: ");gets(fiche.prenom);
printf("AGE: ");scanf("%d",&fiche.age);
flushall();
fwrite(&fiche,sizeof(carte),1,f);
}
}
while((choix=='o')||(choix=='O'));
fclose(f);
}
void recherche(FILE *f,char *n)
{
carte fiche;
int compteur=0;
char trouve = 0,nn[10],pp[10];
clrscr();
printf("RECHERCHE DE FICHE\n\n");
printf("NOM DU FICHIER: ");
gets(n);
flushall();
f = fopen(n,"r");
printf("\nFICHE A RETROUVER:\n");
printf("NOM: ");gets(nn);
printf("PRENOM: ");gets(pp);
flushall();
while((fread(&fiche,sizeof(carte),1,f)!=0)&&(trouve==0))
      {
      if((strcmp(fiche.nom,nn)==0)&&(strcmp(fiche.prenom,pp)==0))
{
trouve=1;
printf("FICHE RETROUVEE: FICHE nø%2d\n",compteur);
}
      compteur++;
      }
if (trouve==0)printf("CETTE FICHE N'EXISTE PAS\n");
fclose(f);
printf("POUR CONTINUER FRAPPER UNE TOUCHE ");
getch();
}
void main()
{
FILE *fichier;
char nom[10];/* nom du fichier */
char choix;
do
{
clrscr();
printf("\t\t\tGESTION DE FICHIER\n");
printf("\t\t\t------------------\n\n\n");
printf("CREATION DU FICHIER  ---> 1\n");
printf("LECTURE DU FICHIER   ---> 2\n");
printf("AJOUTER UNE FICHE    ---> 3\n");
printf("RECHERCHER UNE FICHE ---> 4\n");
printf("SORTIE     ---> S\n\n");
printf("VOTRE CHOIX: ");
choix = (char)getchar();
   flushall();
switch(choix)
{
case '1':creer_fichier(fichier,nom);break;
case '2':lire_fichier(fichier,nom);break;
case '3':ajout(fichier,nom);break;
case '4':recherche(fichier,nom);break;
}
}
while ((choix!='S') && (choix!='s'));
}

Aucun commentaire:

Enregistrer un commentaire