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 94
| // stock.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stock.h"
STOCK *newStock(int nbitem)// fonction mapile voir feuille
{
int i;
STOCK* stock = (STOCK*)malloc(sizeof(STOCK));
stock->nbitem = nbitem;
stock->items = malloc(nbitem * sizeof(ITEM*));
for(i = 0; i < nbitem; i++)
{
stock->items[i] = malloc(sizeof(ITEM*));
}
stock->size = 0;
stock->first = malloc(sizeof(STOCKNODE));
return stock;
}
char *lecture(FILE *fichier,char *buffer)
{
fgets(buffer,256,fichier);
buffer[strlen(buffer)-1] = '\0';//pour nous débarasser du \n;
fgets(buffer,256,fichier);//pour sauter une ligne
return buffer;
}
STOCK *loadStock (char *path)
{
STOCK *monstock;
int i=0,id;
double price;
int nbitem,quantity;
FILE *fichier=NULL;
fichier=fopen("stock.txt","a+");
char *name=malloc(256*sizeof(char));
char *category=malloc(256*sizeof(char));
char *expirationDate=malloc(256*sizeof(char));
char *chaine=malloc(256*sizeof(char));
char *buffer=malloc(256*sizeof(char));
fgets(chaine,256,fichier);
chaine[strlen(chaine)-1] = '\0';//pour nous débarasser du \n;
nbitem=atoi(chaine);
while(i<nbitem)
{
category=lecture(fichier,buffer);
printf("%s",category);
name =lecture(fichier,buffer);
printf("%s",name);
price=atof(lecture(fichier,buffer));
printf("%f\n",price);
i++;
monstock->items[i] = buildItem(i,name,category,price);
}
stockNode*currenTNode=monstock->first;
fgets(buffer,256,fichier);//passe les deux lignes!
fgets(buffer,256,fichier);
while (!feof(fichier))
{
id = atoi(lecture(fichier,buffer)); // l'id
quantity = atoi(lecture(fichier,buffer)); // la quantitée
expirationDate =(lecture(fichier,buffer)); // la date d'expiration
printf("%d\n",id);
printf("%d\n",quantity);
printf("%s\n",expirationDate);
currentStockNode->item = buildStockItem(stock->items[id], quantity, expirationDate); // on fait pointer le champ item de notre node item vers un stockitem que l'on construit avec les données que l'on a lut ds le fichier
stock->size ++;
if(!feof(file)) // si on est pas en fin de fichier, on creer un nouveau stocknode vers lequel next pointera et notre nouveau pointeur de travail deviendra le noeud novellement creer
{
currentNode->next = malloc(sizeof(STOCKNODE));
currentNode = currentNode->next;
}
else // sinon le next du dernier noeud est null;
currentNode->next = NULL;
}
fclose(file);
free(cursor);
return stock;
} |
Partager