Manipulation des fichiers et structures en C
Raptor*
Messages postés
2
Date d'inscription
Statut
Membre
Dernière intervention
-
Raptor* Messages postés 2 Date d'inscription Statut Membre Dernière intervention -
Raptor* Messages postés 2 Date d'inscription Statut Membre Dernière intervention -
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 l'erreur a corrigé
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
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'));
}
merci d'avance voilà mon e-mail --------@-----------.++ (ce n'est pas dans les coutumes du forum de donner l'adresse mail et en même temps tu risques de t'exposer aux spams. Merci. lami20j)
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 l'erreur a corrigé
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
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'));
}
merci d'avance voilà mon e-mail --------@-----------.++ (ce n'est pas dans les coutumes du forum de donner l'adresse mail et en même temps tu risques de t'exposer aux spams. Merci. lami20j)
A voir également:
- Manipulation des fichiers et structures en C
- Renommer des fichiers en masse - Guide
- Fichiers epub - Guide
- Wetransfer gratuit fichiers lourd - Guide
- Explorateur de fichiers - Guide
- Fichiers bin - Guide