Itemio: Items can be loaded from the data file

Signed-off-by: Cestmir Houska <czestmyr@gmail.com>
This commit is contained in:
Cestmir Houska 2011-04-22 03:09:31 +02:00
parent bf838ea046
commit 8b49a62f9e
2 changed files with 52 additions and 3 deletions

View file

@ -1,14 +1,61 @@
#include <stdio.h>
#include <string.h>
#include "itemio.h"
struct item items[ITEM_MAXNUM];
int items_count;
void fill_items()
{
char buf[128];
//Add 32 characters for a safe margin
#define BUFSIZE EAN_MAXLEN + NAME_MAXLEN + 32
char buf[BUFSIZE];
FILE *f = fopen("items.txt", "r");
while (fgets(buf, 128, f)) {
printf("%s\n", buf);
char c;
items_count = 0;
while (fgets(buf, BUFSIZE, f)) {
int i = 0;
int begin = 0;
//Initialize the item
items[items_count].ean[0] = 0;
items[items_count].name[0] = 0;
items[items_count].price = 0;
//Read the item EAN
while (i < BUFSIZE) {
if (buf[i] == '\t') {
buf[i] = 0;
strcpy(items[items_count].ean, &buf[begin]);
buf[i] = '\t';
begin = i+1;
break;
}
i++;
}
//Read the item name
i = begin;
while (i < BUFSIZE) {
if (buf[i] == '\t') {
buf[i] = 0;
strcpy(items[items_count].name, &buf[begin]);
buf[i] = '\t';
begin = i+1;
break;
}
i++;
}
//Use scanf to read the item price
i = begin;
if (i < BUFSIZE) {
sscanf(&buf[i], "%d", &items[items_count].price);
}
items_count++;
}
fclose(f);
}

View file

@ -9,6 +9,8 @@ struct item {
int price;
};
extern int items_count;
extern struct item items[ITEM_MAXNUM];
extern void fill_items();