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
| #include <stdio.h>
#include <stdlib.h>
#define LGL 81 /* Longueur max d'une ligne de texte */
#define LTX 21 /* Longueur max d'un nom de fichier */
#define NPR 30 /* Nombre max de produits */
/*Programmation C, test 2
Le programme charge la liste des produits des son lancement
*/
//Prototype fonction chargeProd
int chargeProd(char nomFichier[LGL], int noProd[], float prProd[], char *t1Prod[NPR], char *t2Prod[NPR]);
int main()
{
char nomFichier [LGL];
int i, n = 0, anzProd = 0;
FILE *fich;
/*Tableaux des produits disponibles*/
/*Tableaux des denominations de produits*/
char *t1Prod [NPR];
char *t2Prod [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, t1Prod, t2Prod);
printf("stop after\n");
for(i=0; i< anzProd;i++)printf("%d %d %f %s %s\n", i, noProd[i], prProd[i], t1Prod[i], t2Prod[i]);
return 0;
}
/*Fonction de lecture des produits*/
int chargeProd(char nomFichier[LGL], int noProd[], float prProd[], char *t1Prod [], char *t2Prod [])
{
int numero = 0, i= 0, n = 0, tst = 0, res = 0;
char nom[LTX], mod[LTX], 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;
t1Prod[i] = nom;
t2Prod[i] = mod;
prProd[i] = prix;
printf("b %d %s %s %f\n", i, t1Prod[i], t2Prod[i], prProd[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("stop %d\n", res );
/*BOUCLE A DETRUIRE
verifie si les produits sont correctement stockes dans la structure*/
for(i=0; i<res;i++)printf("x %d %d %s %s\n", i, noProd[i], t1Prod[i], t2Prod[i]);
}
return res;
} |
Partager