Bonjour a tous,
Voila j'ai un petit problème avec mon programme qui m'affiche une erreur que je ne comprend pas trop, je pense que le problème se situe dans l'ouverture et la fermeture des fichiers mais je n'en suis pas sûr ... Ce programme sert à bidonner des fichiers:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <string.h>
// Structures
struct chantiers
{
long Numchantier;
char NomChantier [25] ;
char rue [50] ;
int CodePostal ;
char ville [20] ;
char MaîtreOuvrage[30] ;
float Montant ;
int Durée ;
char Gérant [3] ;
int position ;
} ;
struct date
{
short Jour;
short Mois;
short Annee;
} ;
struct ouvriers
{
long NumRegistre ;
char Nom [25] ;
char Prenom[30] ;
char Rue [50] ;
int CodePostal ;
char Ville [20] ;
char Specialite [20] ;
struct date d_engag ;
struct date d_naiss ;
} ;
// Prototypes
void Menu();
void Init_Ouvriers(char *);
void Init_Chantiers(char *);
// Fonction principale
void main()
{
int indice = 0, fini=0;
char nom_ouvrier[25] = "Ouvriers.dat";
char nom_chantier[25]= "Chantiers.dat";
while (fini == 0)
{
Menu();
fflush(stdin);
scanf("%d",&indice);
switch(indice)
{
case 1:
system("CLS");
Init_Ouvriers(nom_ouvrier);
Init_Chantiers(nom_chantier);
break;
case 2:
system("CLS");
nom_ouvrier[0] = '\0';
printf("Entrer le nom du fichier Ouvriers (avec l extension .dat): ");
scanf("%s", &nom_ouvrier);
printf("\n");
printf("Entrer le nom du fichier Chantiers (avec l extension .dat): ");
scanf("%s", &nom_chantier);
Init_Ouvriers(nom_ouvrier);
Init_Chantiers(nom_chantier);
break;
case 3:
fini = 1;
break;
default:
system("CLS");
printf("Veuillez ne pas rentrer n'importe quoi .. \n");
Sleep(1500);
system("CLS");
break;
}
}
}
// Fonction Menu
void Menu()
{
system("CLS");
printf("Bienvenue dans ce programme de bidonnage\n");
printf("Que voulez-vous faire?\n");
printf("1. Utiliser les fichiers par default\n");
printf("2. Utilisez d'autres fichiers\n");
printf("3. Quitter le programme\n");
printf("Votre choix: ");
}
// Fonction qui cree le bidonnage du fichier ouvriers
void Init_Ouvriers(char *Fichier_Ouvriers)
{
FILE *f_file;
int i, reponse, fini = 0;
struct ouvriers pouvrier;
while (fini == 0)
{
f_file = fopen(Fichier_Ouvriers,"rb");
if (f_file != NULL)
{
//fclose(f_file);
printf("Le fichier %s existe deja\n",Fichier_Ouvriers);
printf("Que voulez-vous faire?\n");
printf("1. Supprimer et remplacer\n");
printf("2. Ne pas le suprpimer\n");
printf("Votre choix: ");
fflush(stdin);
scanf("%d",&reponse);
}
else
{
// fclose(f_file);
reponse = 1;
}
fclose(f_file);
switch (reponse)
{
case 1:
// Initialisation de table ouvrier
for(i = 0; i < 100; i++)
{
pouvrier.NumRegistre = -1;
pouvrier.Nom[0] = '\0';
pouvrier.Prenom[0] = '\0';
pouvrier.Rue[0] = '\0';
pouvrier.CodePostal = 0;
pouvrier.Ville[0] = '\0';
pouvrier.Specialite[0] = '\0';
pouvrier.d_engag.Jour = 0;
pouvrier.d_engag.Mois = 0;
pouvrier.d_engag.Annee = 0;
pouvrier.d_naiss.Jour = 0;
pouvrier.d_naiss.Mois = 0;
pouvrier.d_naiss.Annee = 0;
}
f_file = fopen(Fichier_Ouvriers,"wb");
if (f_file == NULL)
{
perror("Probleme lors de l ouverture du fichier ouvrier\n");
}
else
{
for (i=0 ; i<100; i++)
fwrite(&pouvrier, sizeof(struct ouvriers),1 , f_file);
}
fclose(f_file);
system("CLS");
printf("Bidonnage du fichier %s effectue",Fichier_Ouvriers);
Sleep(1500);
fini = 1;
break;
case 2:
fini = 1;
break;
default:
fini = 0;
system("CLS");
printf("Ne rentrer pas n'importe quoi ...");
Sleep(1500);
system("CLS");
break;
}
}
}
// Fonction qui crée le bidonnage du fichier chantiers
void Init_Chantiers(char *Fichier_Chantier)
{
int i, reponse, fini = 0;
FILE *f_file;
struct chantiers pchantier;
while (fini == 0)
{
system("CLS");
f_file = fopen(Fichier_Chantier,"rb");
if (f_file != NULL)
{
//fclose(f_file);
printf("Le fichier %s existe deja\n",Fichier_Chantier);
printf("Que voulez-vous faire?\n");
printf("1. Supprimer et remplacer\n");
printf("2. Ne pas le suprpimer\n");
printf("Votre choix: ");
fflush(stdin);
scanf("%d",&reponse);
}
else
{
//fclose(f_file);
reponse = 1;
}
fclose(f_file);
switch (reponse)
{
case 1:
for(i = 0; i<20; i++)
{
pchantier.Numchantier = -1;
pchantier.NomChantier[0] = '\0';
pchantier.rue[0] = '\0';
pchantier.CodePostal = 0;
pchantier.ville[0] = '\0';
pchantier.MaîtreOuvrage[0] = '\0';
pchantier.Montant = 0;
pchantier.Durée = 0;
pchantier.Gérant[0] = '\0';
pchantier.position = 0;
}
f_file = fopen(Fichier_Chantier,"wb");
if (f_file == NULL)
{
perror("Probleme lors de l ouverture du fichier chantier\n");
}
else
{
for (i=0 ; i< 20 ; i++)
fwrite(&pchantier, sizeof(struct chantiers),1 , f_file);
}
fclose(f_file);
system("CLS");
printf("Bidonnage du fichier %s effectue",Fichier_Chantier);
Sleep(1500);
fini = 1;
break;
case 2:
fini = 1;
break;
default:
system("CLS");
printf("Ne rentrer pas n'importe quoi ...");
Sleep(1500);
system("CLS");
fini = 0;
break;
}
}
}
Voici l'erreur:
Merci de m'aider
SbY.
Partager