1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define TAILLE_MAX 100
int lectureDonnees(char* emplacementFichier,
unsigned short int* *ouvertureEntrepot,
unsigned short int** *entrepotDessertCentrale,
unsigned short int *coutOuvertureEntrepot,
unsigned short int** *entrepotCoutLivraisonCentrale)
{
FILE* fichier = NULL;
char chaine[TAILLE_MAX];
char* sousChaine;
unsigned short int nombreEntrepotCentrale;
unsigned short int entrepotLu;
unsigned short int centraleLue;
unsigned short int coutLivraisonLu;
fichier = fopen(emplacementFichier, "r");
if(fichier == NULL)
{
printf(">> Erreur lors de l'ouverture de '%s'", emplacementFichier);
return 1;
} else {
// lecture de la premier ligne
if(fgets(chaine, TAILLE_MAX, fichier) == NULL)
{
printf(">> Le format des données du fichier sont incorrectes");
return 1;
} else {
sousChaine = strtok(chaine, " ");
if(sousChaine != NULL)
{
nombreEntrepotCentrale = atoi(sousChaine);
*ouvertureEntrepot = calloc(nombreEntrepotCentrale, sizeof(unsigned short int));
*entrepotDessertCentrale = malloc(nombreEntrepotCentrale * sizeof(unsigned short int*));
for(int i = 0; i < nombreEntrepotCentrale; i ++)
{
entrepotDessertCentrale[i] = calloc(nombreEntrepotCentrale, sizeof(unsigned short int));
}
*entrepotCoutLivraisonCentrale = malloc(nombreEntrepotCentrale * sizeof(unsigned short int*));
for(int i = 0; i < nombreEntrepotCentrale; i ++)
{
entrepotCoutLivraisonCentrale[i] = malloc(nombreEntrepotCentrale * sizeof(unsigned short int));
for(int j = 0; j < nombreEntrepotCentrale; j ++)
{
entrepotCoutLivraisonCentrale[i][j] = USHRT_MAX;
}
}
}
sousChaine = strtok(NULL, " ");
if(sousChaine != NULL)
{
*coutOuvertureEntrepot = atoi(sousChaine);
}
}
// lecture des autres lignes
while(fgets(chaine, TAILLE_MAX, fichier) != NULL)
{
sousChaine = strtok(chaine, " ");
if(sousChaine != NULL)
{
entrepotLu = atoi(sousChaine);
}
sousChaine = strtok(NULL, " ");
if(sousChaine != NULL)
{
centraleLue = atoi(sousChaine);
}
sousChaine = strtok(NULL, " ");
if(sousChaine != NULL)
{
coutLivraisonLu = atoi(sousChaine);
}
entrepotDessertCentrale[entrepotLu - 1][centraleLue - 1] = 1;
entrepotCoutLivraisonCentrale[entrepotLu - 1][centraleLue - 1] = coutLivraisonLu;
}
fclose(fichier);
}
return 0;
} |
Partager