Demande d'aide (Gestion des fichiers et structures en C)
medsky
Messages postés1Date d'inscriptionvendredi 10 décembre 2010StatutMembreDernière intervention 2 juin 2011
-
2 juin 2011 à 21:45
cs_patatalo
Messages postés1466Date d'inscriptionvendredi 2 janvier 2004StatutModérateurDernière intervention14 février 2014
-
3 juin 2011 à 09:35
Bonjour,
Afin de manipuler les fichiers et les structures en C, On m'a demandé de réalisé une application qui cree un fichier pour saisir les informations des étudiants ( Nom / prénom / CNE) avec la possibilité de d'ajout /suppression /recherche.
qq peut m'aider a trouver les erreurs a corrigé dans ce code etmerci d'avance
typedef struct
{
char nom[10];
char prenom[10];
int CNE;
}
carte; /* creation de la carte d informations*/
//*********************************************************************//
//*********************** La creation du fichier*****************************//
//*********************************************************************//
void creer_fichier(FILE *f,char *n)
{
char choix;
carte fiche;
system("cls");
system("color E4");
printf(" ************* Creation du fichier ***************** \n\n");
printf(" Nom du fichier a cree: ");
gets(n);
f = fopen(n,"w");
do
{
printf("\n Saisir une fiche d information ?(o/n) ");
choix = (char)getchar();
if ((choix=='o')||(choix=='O'))
{
printf("\nNOM: ");
gets(fiche.nom);
printf("\nPRENOM: ");
gets(fiche.prenom);
printf("CNE: ");
scanf("%d",&fiche.CNE);
fwrite(&fiche,sizeof(carte),1,f);
}
}
while((choix=='o')||(choix=='O'));
fclose(f);
}
//**********************************************************************//
//*************************La lecture du fichier*****************************//
//*********************************************************************//
void lire_fichier(FILE *f,char *n)
{
carte fiche;
int compteur=0;
system("cls");
system("color E4");
printf("*************** Lecture du fichier ***************\n\n");
printf("Nom du fichier a lire: ");gets(n);
f = fopen(n,"r");
if (f == NULL)
printf("\n ERREUR, ce fichier n'existe pas :-( \n\n");
else
{
printf("\nLISTING DU FICHIER\n\n");
while(fread(&fiche,sizeof(carte),1,f)!=0)
{
printf("fiche noeud: \n",compteur);
compteur++;
printf("%s %s %d an(s)\n\n",fiche.nom,fiche.prenom,fiche.CNE);
}
fclose(f);
}
printf("Pour continuer tapper une touche svp ");
getch();
}
//**********************************************************************//
//************************ lA commande d ajout ****************************//
//**********************************************************************//
void ajout(FILE *f,char *n)
{
carte fiche;
char choix;
system("cls");
system("color E4");
printf("********** Ajout d une fiche d information *********** \n\n");
printf("Nom du fichier a modifier: ");
gets(n);
f = fopen(n,"a");
do
{
printf("\n Saisir une fiche d information ?(o/n) ");
choix = (char)getchar();
if ((choix=='o')||(choix=='O'))
{
printf("\nNOM: ");gets(fiche.nom);
printf("PRENOM: ");gets(fiche.prenom);
printf("CNE: ");scanf("%d",&fiche.CNE);
fwrite(&fiche,sizeof(carte),1,f);
}
}
while((choix=='o')||(choix=='O'));
fclose(f);
}
//*********************************************************************//
//********************** La commande de recherche ***********************//
//********************************************************************//
void recherche(FILE *f,char *n)
{
carte fiche;
int compteur=0;
char trouve = 0,nn[10],pp[10];
system("cls");
printf("Recherche du fiche d information \n\n");
printf("Nom du fichier: ");
gets(n);
f = fopen(n,"r");
printf("\n Fichier a retrouver :\n");
printf("NOM: ");gets(nn);
printf("PRENOM: ");gets(pp);
while((fread(&fiche,sizeof(carte),1,f)!=0)&&(trouve==0))
{
if((strcmp(fiche.nom,nn)==0)&&(strcmp(fiche.prenom,pp)==0))
{
trouve=1;
printf("fichier trouve: FICHE noeud\n",compteur);
}
compteur++;
}
if (trouve==0)printf("Oh ! la fiche n exist pas :-( \n");
fclose(f);
printf("Taper une touche pour continuer");
getch();
}
//**********************************************************************//
//**************************** Le Menu *********************************//
//**********************************************************************//
int main()
{
FILE *fichier;
char nom[10];/* nom du fichier */
char choix;
do
{
system("color f1");
printf("\n");
printf("\n");
printf("\n");
printf("\t*************************************************************");
printf("\n\t*************************************************************");
printf("\n\t* *");
printf("\n\t* CREATION DU FICHIER ---> 1 *");
printf("\n\t* LECTURE DU FICHIER ---> 2 *");
printf("\n\t* AJOUTER UNE FICHE D INFORMATION ---> 3 *");
printf("\n\t* RECHERCHER UNE FICHE ---> 4 *");
printf("\n\t* SORTIE ---> S *");
printf("\n\t* VOTRE CHOIX: *");
printf("\n\t* *");
printf("\n\t* *");
printf("\n\t************************************************************");
printf("\n\t************************************************************");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
choix = (char)getchar();
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'));
}
priere de répondre
A voir également:
Demande d'aide (Gestion des fichiers et structures en C)
cs_patatalo
Messages postés1466Date d'inscriptionvendredi 2 janvier 2004StatutModérateurDernière intervention14 février 20142 3 juin 2011 à 09:35
salut,
Il faut que tu ouvres le fichier (fopen) en format binaire et non texte. Vu le fonctionnement de ton prog, il n'est pas nécessaire d'utiliser les streams, tu ne t'en sert pas.
f << carte.pchar << "prenom" << "3432345";
fgets()...
PS: Faut pas s'appeler marie-antoinette avec ton prog :) Soit tu limites les entrées clavier à 10, soit tu testes que la taille soit correcte par passage dans un buffer intermediaire avant de sauvegarder.