- Steve - wrote:[color=blue]
> I've been given a school assignment and while everything else is easy
> there's one topic I'm completley lost on.
>
> I've been given an ASCII file that looks like this. During start-up, the
> program needs to read an ASCII data file of inventory records and build an
> appropriate dynamic data structure using structs and pointers.
>
> 10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
> 10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
> 10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
> 10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00
>
> The format is:
>
> 5 digits Item Number
> space
> 20 chars Item description
> space
> 4 digits Quantity
> space
> 1 digit Category code
> space
> ....... and so on.
>
> What should my strucutre look like? I was thinking I could just use an
> array of length 55 or so. But that doesn't seem to fit the requirment for a
> strucutre. Why not just have a two dimensional array then. The strucutre
> could have variable for each item (Item Number, Item Description, etc), but
> how can I parse the file correctly then?
>[/color]
I would make a struct called INVENTORY like this:
typedef struct ITEM
{
unsigned num; // Item No.
char desc[21]; // Description
unsigned quan; // Quantity on hand
char cat; // Catorgory
double cost; // Cost
double price; // Selling price
} ITEM;
typedef struct INVENTORY
{
unsigned total; // Total Items in Inventory
ITEM* item; // Array of items
} INVENTORY;
Write functions to manipulate the array of items.
For example, write a function AddItemToInv could add an item
from the file to the inventory array.
You could use function fscanf (or sscanf) to read the line from
the file using the format string "%u %20s %u %c %lf %lf".
You could use function fprintf (or sprintf) to write a line to
the file using format string "%05u %s %04u %c %.2f %.2f"
An example using functions sscanf and sprintf:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ITEM
{
unsigned num;
char desc[21];
unsigned quan;
char cat;
double cost;
double price;
} ITEM;
typedef struct INVENTORY
{
unsigned total;
ITEM* item;
} INVENTORY;
ITEM *AddItemToInv(INVENTORY *p, const char *s);
int main(void)
{
char item[80] = "10235 CHARACTER_PRINTER 4013 "
"C 000995.00 001295.00";
INVENTORY inventory = {0,NULL};
ITEM *tmp;
printf("The line in the file would be:\n"
"\"%s\"\n\n",item);
puts("Using function AddItemToInv to add to inventory array\n");
if((tmp = AddItemToInv(&inventory,item)) != NULL)
{
/* change description and item number */
puts("Changing the description and item number...");
tmp->num = 6205;
strcpy(tmp->desc,"GRAPHIC_PRINTER");
printf("Item No. %05d is a %s\n\n",inventory.item[0].num,
inventory.item[0].desc);
sprintf(item,"%05u %s %04u %c %.2f %.2f",tmp->num,
tmp->desc, tmp->quan, tmp->cat, tmp->cost, tmp->price);
printf("Using function sprintf or fprintf to save to the file "
"as:\n\"%s\"\n",item);
}
else puts("Unable to add item to inventory");
free(inventory.item);
return 0;
}
ITEM *AddItemToInv(INVENTORY *p, const char *s)
{
ITEM *tmp;
unsigned inv_num = p->total;
if((tmp = realloc(p->item, sizeof(*tmp)*(p->total+1))) == NULL)
return NULL; /* memory allocation error */
p->item = tmp;
if(6 != sscanf(s,"%u %20s %u %c %lf %lf",
&tmp->num,tmp->desc,&tmp->quan,
&tmp->cat,&tmp->cost,&tmp->price))
return NULL; /* file(string) format error */
p->item[p->total++] = *tmp;
return &p->item[inv_num];
}
--
Al Bowers
Tampa, Fl USA
mailto:
xal@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/