Les Fichiers sur C
maya89
Messages postés
22
Date d'inscription
Statut
Membre
Dernière intervention
-
jakfacia -
jakfacia -
Bonjour,
j'ai un programme qui permet d'ajouter et de rechercher un commercant ... mais lors de l'appelle de la fonction recherche le programme se bloque... voici son code :
# include <conio.h>
# include <stdio.h>
float Sf,ca;
int mat,nbrCommercant=0;
FILE* f;
struct commercant
{
char nom[20];
char prenom[20];
float salaire;
int matricule;
}com,tabcom;
int menu();
float salireNet(float Sf,float ca);
void ajoutCommercant();
void remplirTab( struct commercant Tabcom[10],struct commercant com);
void enregistrerCommercant(struct commercant Tabcom[10]);
struct commercant saisie();
int rechercheCommercant(struct commercant com,int mat);
float prime(float ca);
/************************ MAIN *****************/
int main()
{
int i;
i=menu();
switch(i)
{
case 1 : ajoutCommercant();break;
case 4 : printf("saisir le matricule du commercant\n");
scanf("%d",mat);
rechercheCommercant(com,mat);break;
}
getchar();
}
/*********************** MENU ******************/
int menu()
{
int choix;
do
{
printf("~~~~~~~~~~ MENU ~~~~~~~~~~\n");
printf(" 1 : Ajouter un commercant \n");
printf(" 2 : Modifier les informations d'un commercant \n");
printf(" 3 : Supprimer les informations d'un commercant \n");
printf(" 4 : Rechercher un commercant \n");
printf(" 5 : Afficher les commercants\n");
printf(" 6 : Quitter\n");
printf(" Faites votre choix\n");
scanf("%d",&choix);
}while((choix<1)||(choix>6));
return(choix);
}
/********************* AJOUT ******************/
void ajoutCommercant()
{
char rep;
//int c;
struct commercant com,Tabcom[10];
//rep='o';
do
{
com = saisie();
remplirTab(Tabcom,com);
printf("voulez vous ajouter un autre commercant? \n");
rep = getch();//scanf("%c",&rep);
} while(rep!='n');
enregistrerCommercant(Tabcom);
}
/******************* SAISIE ********************/
struct commercant saisie()
{
char rep;
printf("saisir le nom du commercant\n");
scanf("%s",com.nom);
printf("saisir le prenom du commercant\n");
scanf("%s",com.prenom);
printf("saisir le matricule du commercant\n");
scanf("%d",&com.matricule);
printf("saisir le chiffre d'affaire du commercant\n");
scanf("%f",&ca);
prime(ca);
printf("saisir le salaire fixe du commercant\n");
scanf("%f",&Sf);
com.salaire=salireNet(Sf,ca);
return(com);
}
/****************** PRIME *********************/
float prime(float ca)
{
float p;
if(ca>50000)
{
p=ca*0.1;
}
else
{
p=ca*0.03;
}
return(p);
}
/************** SALAIRE_NET ********************/
float salireNet(float Sf,float ca)
{
float Sn;
Sn=(Sf+prime(ca));
return(Sn);
}
/********** REMPLIR TABLEAU***********/
void remplirTab( struct commercant Tabcom[10],struct commercant com)
{
Tabcom[nbrCommercant]=com;
nbrCommercant++;
}
/********* ENREGISTRER **************/
void enregistrerCommercant(struct commercant Tabcom[10])
{
int n;
FILE* f;
f=fopen("c:\\Commercant.txt","w");
if (f != NULL)
{
n=sizeof(com);
printf ("%d", nbrCommercant);
getchar ();
fwrite(&Tabcom,n,nbrCommercant - 1,f);
fclose(f);
}
else
{
printf ("problemme d'ouverture du fichier");
}
}
/********************* RECHERCHER ***************/
int rechercheCommercant(struct commercant com,int mat)
{
int trouve,i,n;
n=1;
i=n*sizeof(com);
trouve=0;
f=fopen("commercant.txt","r");
if (f != NULL)
{
while((i!=feof(f))&&(trouve==0))
{
fseek(f,i,0);
fread(&com,sizeof(com),1,f);
if(com.matricule=mat)
{
printf("%s",com.nom);
printf("%s",com.prenom);
printf("%d",com.matricule);
printf("%f",com.salaire);
trouve=1;
}
else
{
n=n+1;
}
}
}
else
printf("probleme lors de l'ouverture du fichier\n");
fclose(f);
return(trouve);
}
pourriez vous m'aider???
merci d'avance
j'ai un programme qui permet d'ajouter et de rechercher un commercant ... mais lors de l'appelle de la fonction recherche le programme se bloque... voici son code :
# include <conio.h>
# include <stdio.h>
float Sf,ca;
int mat,nbrCommercant=0;
FILE* f;
struct commercant
{
char nom[20];
char prenom[20];
float salaire;
int matricule;
}com,tabcom;
int menu();
float salireNet(float Sf,float ca);
void ajoutCommercant();
void remplirTab( struct commercant Tabcom[10],struct commercant com);
void enregistrerCommercant(struct commercant Tabcom[10]);
struct commercant saisie();
int rechercheCommercant(struct commercant com,int mat);
float prime(float ca);
/************************ MAIN *****************/
int main()
{
int i;
i=menu();
switch(i)
{
case 1 : ajoutCommercant();break;
case 4 : printf("saisir le matricule du commercant\n");
scanf("%d",mat);
rechercheCommercant(com,mat);break;
}
getchar();
}
/*********************** MENU ******************/
int menu()
{
int choix;
do
{
printf("~~~~~~~~~~ MENU ~~~~~~~~~~\n");
printf(" 1 : Ajouter un commercant \n");
printf(" 2 : Modifier les informations d'un commercant \n");
printf(" 3 : Supprimer les informations d'un commercant \n");
printf(" 4 : Rechercher un commercant \n");
printf(" 5 : Afficher les commercants\n");
printf(" 6 : Quitter\n");
printf(" Faites votre choix\n");
scanf("%d",&choix);
}while((choix<1)||(choix>6));
return(choix);
}
/********************* AJOUT ******************/
void ajoutCommercant()
{
char rep;
//int c;
struct commercant com,Tabcom[10];
//rep='o';
do
{
com = saisie();
remplirTab(Tabcom,com);
printf("voulez vous ajouter un autre commercant? \n");
rep = getch();//scanf("%c",&rep);
} while(rep!='n');
enregistrerCommercant(Tabcom);
}
/******************* SAISIE ********************/
struct commercant saisie()
{
char rep;
printf("saisir le nom du commercant\n");
scanf("%s",com.nom);
printf("saisir le prenom du commercant\n");
scanf("%s",com.prenom);
printf("saisir le matricule du commercant\n");
scanf("%d",&com.matricule);
printf("saisir le chiffre d'affaire du commercant\n");
scanf("%f",&ca);
prime(ca);
printf("saisir le salaire fixe du commercant\n");
scanf("%f",&Sf);
com.salaire=salireNet(Sf,ca);
return(com);
}
/****************** PRIME *********************/
float prime(float ca)
{
float p;
if(ca>50000)
{
p=ca*0.1;
}
else
{
p=ca*0.03;
}
return(p);
}
/************** SALAIRE_NET ********************/
float salireNet(float Sf,float ca)
{
float Sn;
Sn=(Sf+prime(ca));
return(Sn);
}
/********** REMPLIR TABLEAU***********/
void remplirTab( struct commercant Tabcom[10],struct commercant com)
{
Tabcom[nbrCommercant]=com;
nbrCommercant++;
}
/********* ENREGISTRER **************/
void enregistrerCommercant(struct commercant Tabcom[10])
{
int n;
FILE* f;
f=fopen("c:\\Commercant.txt","w");
if (f != NULL)
{
n=sizeof(com);
printf ("%d", nbrCommercant);
getchar ();
fwrite(&Tabcom,n,nbrCommercant - 1,f);
fclose(f);
}
else
{
printf ("problemme d'ouverture du fichier");
}
}
/********************* RECHERCHER ***************/
int rechercheCommercant(struct commercant com,int mat)
{
int trouve,i,n;
n=1;
i=n*sizeof(com);
trouve=0;
f=fopen("commercant.txt","r");
if (f != NULL)
{
while((i!=feof(f))&&(trouve==0))
{
fseek(f,i,0);
fread(&com,sizeof(com),1,f);
if(com.matricule=mat)
{
printf("%s",com.nom);
printf("%s",com.prenom);
printf("%d",com.matricule);
printf("%f",com.salaire);
trouve=1;
}
else
{
n=n+1;
}
}
}
else
printf("probleme lors de l'ouverture du fichier\n");
fclose(f);
return(trouve);
}
pourriez vous m'aider???
merci d'avance
A voir également:
- Les Fichiers sur C
- Renommer des fichiers en masse - Guide
- Fichiers epub - Guide
- Wetransfer gratuit fichiers lourd - Guide
- Explorateur de fichiers - Guide
- Fichiers bin - Guide