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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LGL 81 /* Longueur max d'une ligne de texte */
#define LCH 20 /* Longueur max de la chaine */
#define LTX 22 /* Longueur max de la chaine avec \n */
#define NPR 30 /* Nombre max de produits */
/*Programmation C, test 2
//Prototype fonction chargeProd
int chargeProd(char nomFichier[LGL], int noProd[], float prProd[], char *markProd[NPR], char *modlProd[NPR]);
int main()
{
char nomFichier [LGL];
int i, n = 0, anzProd = 0;
FILE *fich;
/*Tableaux des produits disponibles*/
/*Tableaux des denominations de produits*/
char markProd [LTX][NPR];
char modlProd [LTX][NPR];
/*Tableaux des donnees numeriques de produits*/
int noProd [NPR];
/*Tableaux des prix de produits*/
float prProd [NPR];
/*Appel de la fonction de lecture des produits*/
anzProd = chargeProd("produits.txt", noProd, prProd, markProd, modlProd);
printf("retour main\n");
/*BOUCLE DE LECTURE MAIN
verifie si les produits sont correctement stockes dans les tables*/
for(i=0; i< anzProd;i++)printf("Iter Main %d %d %f %s %s\n", i, noProd[i], prProd[i], markProd[i], modlProd[i]);
return 0;
}
/*Fonction de lecture des produits*/
int chargeProd(char nomFichier[LGL], int noProd[], float prProd[], char *markProd[NPR], char *modlProd[NPR])
{
int numero = 0, i= 0, n = 0, tst = 0, res = 0;
char nom[LCH], mod[LCH], ligne [LGL];
float prix;
FILE *fich;
/* Ouverture du fichier a lire*/
fich = fopen (nomFichier, "r");
if ( fich == NULL ) {
printf ("Ouverture impossible fichier %s\n", nomFichier);
} else {
fgets (ligne, LGL, fich); // Lecture 1ère ligne
while (!feof(fich)) { // Tant que non fin de fichier
// boucle de lecture et stockage des informations produit dans la structure
n = sscanf (ligne, "%d %s %s %f", &numero, nom, mod, &prix) ;
if (n==4 && tst == 0){
noProd[i] = numero;
strcpy(markProd[i], nom);
strcpy(modlProd[i], mod);
prProd[i] = prix;
printf("Iter Boucle %d %d %f %s %s\n", i, noProd[i], prProd[i], markProd[i], modlProd[i] );
i++;
res = i;
}
fgets (ligne, LGL, fich); // Lecture ligne suivante
if ( i >= NPR) { tst = 1; res = NPR; } // max atteint --> sortie de boucle avec message
}
if (tst ==1) {printf ("Trop d'elements dans la liste %s\n", nomFichier);}
fclose (fich);
printf("fin de lecture\n");
/*BOUCLE DE LECTURE FONCTION
verifie si les produits sont correctement stockes dans les tables*/
for(i=0; i<res;i++)printf("Iter Sortie %d %d %f %s %s\n", i, noProd[i], prProd[i], markProd[i], modlProd[i]);
}
return res;
} |
Partager