473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Structure and ASCII "Database"

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?
Nov 13 '05 #1
13 4283
> 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?


As far as I understand, each line in the ASCII code represents a record. You
could define a structure in which each item is represented in the structure.
The structure could also contain a pointer to structure so that you can use
it to build up a linked list. Read each item one by one and store them in
the items. If the items have fixed length, you could use that information
for reading in the data.

Regards,
Nathan
Nov 13 '05 #2
On Sat, 06 Sep 2003 18:28:21 GMT, "- Steve -"
<se****@foundation.sdsu.edu> wrote:
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.
We call these fields, so each line of the data file represents one
record, each record has 7 fields. It is easier to handle fixed
width fields so I'll assume that for example "Item description"
can be up to 20 characters in length.
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?


#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription[ MAX_LEN_ITEM_DESCRIPTION + 1 ];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

Note that I use integer types for the 6th and 7th fields, although
the numbers actually have two decimal places. This avoids having
to use a floating point type which could cause rounding errors.
So for example the value "000008.50" in the file would be stored
as the integer 850.

If you know the maximum number of records in the file then define an
array to hold all the records:

#define MAX_RECORDS 10000
struct record DataFile[ MAX_RECORDS ];

Nick.

Nov 13 '05 #3

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:9s********************************@4ax.com...
On Sat, 06 Sep 2003 18:28:21 GMT, "- Steve -"
<se****@foundation.sdsu.edu> wrote:
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 anappropriate 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.


We call these fields, so each line of the data file represents one
record, each record has 7 fields. It is easier to handle fixed
width fields so I'll assume that for example "Item description"
can be up to 20 characters in length.
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 astrucutre. Why not just have a two dimensional array then. The strucutrecould have variable for each item (Item Number, Item Description, etc), buthow can I parse the file correctly then?


#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription[ MAX_LEN_ITEM_DESCRIPTION + 1 ];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

Note that I use integer types for the 6th and 7th fields, although
the numbers actually have two decimal places. This avoids having
to use a floating point type which could cause rounding errors.
So for example the value "000008.50" in the file would be stored
as the integer 850.

If you know the maximum number of records in the file then define an
array to hold all the records:

#define MAX_RECORDS 10000
struct record DataFile[ MAX_RECORDS ];

Nick.

I understand that, but how do I import the data from the ASCII table into
the variables.

So I declare a variable:

record myData[100]; //for example

Now how do I enter the first line into myData[0]? Do I use cin to enter the
entire line into a string, then parse that array based on spaces?
Nov 13 '05 #4

- Steve - <se****@foundation.sdsu.edu> wrote in message
news:Ji*******************@news1.central.cox.net.. .

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:9s********************************@4ax.com...
On Sat, 06 Sep 2003 18:28:21 GMT, "- Steve -"
<se****@foundation.sdsu.edu> wrote:
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, theprogram needs to read an ASCII data file of inventory records and build anappropriate 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.
We call these fields, so each line of the data file represents one
record, each record has 7 fields. It is easier to handle fixed
width fields so I'll assume that for example "Item description"
can be up to 20 characters in length.
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 astrucutre. Why not just have a two dimensional array then. The strucutrecould have variable for each item (Item Number, Item Description, etc), buthow can I parse the file correctly then?


#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription[ MAX_LEN_ITEM_DESCRIPTION + 1 ];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

Note that I use integer types for the 6th and 7th fields, although
the numbers actually have two decimal places. This avoids having
to use a floating point type which could cause rounding errors.
So for example the value "000008.50" in the file would be stored
as the integer 850.

If you know the maximum number of records in the file then define an
array to hold all the records:

#define MAX_RECORDS 10000
struct record DataFile[ MAX_RECORDS ];

Nick.

I understand that, but how do I import the data from the ASCII table into
the variables.

So I declare a variable:

record myData[100]; file://for example

Now how do I enter the first line into myData[0]?


Read it from the file.
Do I use cin to enter the
C has no such thing as 'cin'. Did you mean to post
this to a C++ group?
entire line into a string, then parse that array based on spaces?


See standard library function 'fgets()' or 'fscanf()'

-Mike

Nov 13 '05 #5
"- Steve -" <se****@foundation.sdsu.edu> wrote:

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:9s********************************@4ax.com.. .
<SNIP>
#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription[ MAX_LEN_ITEM_DESCRIPTION + 1 ];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

Note that I use integer types for the 6th and 7th fields, although
the numbers actually have two decimal places. This avoids having
to use a floating point type which could cause rounding errors.
So for example the value "000008.50" in the file would be stored
as the integer 850.

If you know the maximum number of records in the file then define an
array to hold all the records:

#define MAX_RECORDS 10000
struct record DataFile[ MAX_RECORDS ];


I understand that, but how do I import the data from the ASCII table into
the variables.

So I declare a variable:

record myData[100]; //for example


make this: struct record myData[100];

Now how do I enter the first line into myData[0]? Do I use cin to enter the
entire line into a string, then parse that array based on spaces?


'cin' is a C++ construct; this newsgroup is about C, so here is a C
approach to your problem:

1. open the file via fopen()

2. read a whole line into a buffer from your file, using fgets()

3. break the line into the individual fields; in your case the fields
are seperated by easy to spot white spaces.

4. while doing so, copy the individual parts into the members of your
struct, eventually converting them to the required type

5. repeat 2. - 4. until there's no more data left

6. close the file via fclose()

To get an idea about how to achieve all this, you should look up the
following functions in your reference book (or online help, or whatever
you use):

fopen(), fclose(), fgets(),
sscanf(), strchr(), strtok(), strncpy(), strtoul()

If you get stuck somewhere, post the code you have so far and be assured
that someone here will help you get back on track.

Irrwahn

--
Posting in usenet is like playing golf in a mine-field.
Nov 13 '05 #6
In 'comp.lang.c', "- Steve -" <se****@foundation.sdsu.edu> wrote:
I've been given a school assignment and while everything else is easy
there's one topic I'm completley lost on.
Better to post your attempts if you want an efficient help...
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.
This means that the so-called 'ASCII format' (Text or Space format could be a
better name) is actually defined in termes of 'maximum size fields separated
with a space' rather that 'fixed size fields'.
What should my strucutre look like?
(WARNING, This is mangled C. If you are really lost, use ROT-13 for decoding,
but work on your assigment yourself first.)

To stick to your definition :

glcrqrs fgehpg
{
pune Vgrz_Ahzore [5 + 1];
pune Vgrz_qrfpevcgvba [20 + 1];
pune Dhnagvgl [4 + 1];
pune Pngrtbel_pbqr [1 + 1];
}
erpbeq_g;
The
strucutre could have variable for each item (Item Number, Item
Description, etc), but how can I parse the file correctly then?


Then, you read the line with fgets():

pune yvar [fvmrbs erpbeq_g + fvmrbs "\a"];

strgf (yvar, fvmrbs yvar, svyr_cbvagre);

And finally parse the line to the structure:

erpbeq_g e = {0};

ffpnas (yvar, "%f %f %f %f"
, e.Vgrz_Ahzore
, e.Vgrz_qrfpevcgvba
, e.Dhnagvgl
, e.Pngrtbel_pbqr
);

Check the return value (must be 4) before continuing.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #7
In 'comp.lang.c', "- Steve -" <se****@foundation.sdsu.edu> wrote:
So I declare a variable:

record myData[100]; //for example

Now how do I enter the first line into myData[0]? Do I use cin to enter
the entire line into a string, then parse that array based on spaces?


Something like that, except that in C, we use fgets() ans sscanf() for
example. 'cin' is not a C-thing.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #8

"- Steve -" <se****@foundation.sdsu.edu> wrote in message
I understand that, but how do I import the data from the ASCII table > into the variables.
So I declare a variable:

record myData[100]; file://for example

Now how do I enter the first line into myData[0]? Do I use cin to
enter the entire line into a string, then parse that array based on
spaces?

In my opinion your teacher shouldn't have set this exercise, as doing
everything properly is too hard for where you're at.

Firstly you need a structure for your records. This is usually the place you
start in computer programming - defining your basic data structures. The
main choice you must make is between a fixed-length field for the name,
or a variable-length field. A variable length field would be more robust.

Now you need to code a loadrecord() function.

This is

/*
loadrecord - load a record from an ascii file.
Params: rec - pointer to the record to load
fp - pointer to an open file
Returns: 0 on success, -1 on failure.
*/
int loadrecord(record *rec, FILE *fp)

You write the function using fscanf(), checking the return value and
returning -1 if an error is encountered. This is crucial. Never assume that
input is not corrupted.

If you know how many records you have, you can input the data like this.

int main(void)
{
FILE *fp;
record myData[100];
int i;

fp = fopen("myfile.txt", "r");
if(!fp)
/* abort here */

for(i=0;i<100;i++)
{
err = loadrecord(&myData[i], fp);
if(err == -1)
{
/* print an error message here and abort */
}
}

fclose(fp);

/* now you do the rest of the program with data in the array myData */
}

However in a real program you would be unlikely to know that the number of
records is exactly 100. This is where things get difficult. You need to
declare myData as a pointer to data allocated with malloc(), use realloc()
to expand the array as you read data in, and you need some way of telling
when all the lines in the file have been read.

This is why I think it is a bad assignment, since it is quite difficult to
do all these things cleanly.
Nov 13 '05 #9
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bj**********@newsg3.svr.pol.co.uk...

Now you need to code a loadrecord() function.

This is

/*
loadrecord - load a record from an ascii file.
Params: rec - pointer to the record to load
fp - pointer to an open file
Returns: 0 on success, -1 on failure.
*/
int loadrecord(record *rec, FILE *fp)

int main(void)
{
FILE *fp;
record myData[100];
int i;

fp = fopen("myfile.txt", "r");
if(!fp)
/* abort here */

for(i=0;i<100;i++)
{
err = loadrecord(&myData[i], fp);
if(err == -1)
{
/* print an error message here and abort */
}
}

fclose(fp);

/* now you do the rest of the program with data in the array myData */
}


So this newsgroup is now the "I'll do your homework for you" group? Since
when do we write code for students. I think you all should back off a little
and let the kid figure it out himself.

--
ArWeGod
Nov 13 '05 #10


- Steve - wrote:
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?


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: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #11


richard wrote:
On Mon, 08 Sep 2003 08:37:21 -0400, Al Bowers <xa*@abowers.combase.com> wrote:
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)

Consider malloc'ing a larger block of memory and only calling realloc when that
block is used up, rather than frequent calls to realloc.

Many prefer not putting parens after sizeof, as they deem it significant to
distinguish size(type) and sizeof something-other-than-type.

ymmv.

At least, those are comments I got when posting similar code. Code on which you
were kind enough to comment, if memory serves.


I guess you are referring to the "memory mgmt problem" thread. I made
comments on your post but not regarding the two issues above.

The first issue you mentioned is valid. In code that will have
frequent reallocations one would want to improve the efficiency by
allocating blocks. However, Dealing with blocks brings up an additional
issue of determining the most efficient block size.
The sample code presented here is trival and I did not want to muddle
with block allocations.

The second issue IMO deals with preference. To me perspicuity is
better achieved with:
realloc(p->item, sizeof(*tmp)*(p->total+1))
as I look at it as (sizeof the an object)*(number of objects).
I would not do:
realloc(p->item, sizeof*tmp*(p->total+1))
and probable not do
realloc(p->item,(p->total+1)*sizeof*tmp)
in my code.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #12
"Al Bowers" <xa*@abowers.combase.com> wrote in message
news:bj************@ID-169908.news.uni-berlin.de...


- Steve - wrote:
I've been given a school assignment and while everything else is easy
there's one topic I'm completley lost on.

....snip...
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: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/


So this newsgroup is now the "I'll do your homework for you" group? Since
when do we write code for students. I think you all should back off a little
and let the kid figure it out himself.

--
ArWeGod

Nov 13 '05 #13


ArWeGod wrote:
"Al Bowers" <xa*@abowers.combase.com> wrote in message
news:bj************@ID-169908.news.uni-berlin.de...

- Steve - wrote:
I've been given a school assignment and while everything else is easy
there's one topic I'm completley lost on.
Steve - also wrote:
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
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;
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"


...........code snipped........
So this newsgroup is now the "I'll do your homework for you" group? Since
when do we write code for students. I think you all should back off a little
and let the kid figure it out himself.


Perhaps it was too much code but certainly not "I'll do your homework".
I deliberately made the struct ITEM containing six members. You can see
from the data file example that he will need 7 members. So the op will
need to change the struct, modify the fscanf(sscanf) and fprintf
(sprintf) functions. Also, code was not included that would open and
read the data file.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robin Tucker | last post by:
I have some code that dynamically creates a database (name is @FullName) and then creates a table within that database. Is it possible to wrap these things into a transaction such that if any one...
1
by: Yasso Picasso | last post by:
Greetings, I have to admit that I'm still a beginner in the database field, but I'm actively studying, and this is why I will be utterly grateful for the proper, accurate, and wise guidance to...
1
by: Kenjis Kaan | last post by:
I had to run DB2 on Win2k. After installation it puts a directory under c:\DB2 and c:\DB2Log and C:\Program Files\SQLLIB Now am all confused which is instance home, db2 home etc. I had to...
4
by: (Pete Cresswell) | last post by:
I would argue that it is not. JET is a desktop DB engine. Sybase is a database Oracle is a database DB2 is a database. VB 6 is a front-end development tool. PowerBuilder is a front-end...
3
by: Arnold | last post by:
I am having problem loading the image from the database. It gives this error: "Invalid parameter used." This is my source code: Private abyt() As Byte Private fo As New OpenFileDialog Private sf...
7
by: Risen | last post by:
Hi,all, I want to execute SQL command " DROP DATABASE mydb" and "Restore DATABASE ....." in vb.net 2003. But it always shows error. If any body can tell me how to execute sql command as above?...
0
by: serge | last post by:
I have 2 SQL Enterprise Editions running SP2 and same collation. When i try to start database mirroring I get the following error message: "The remote copy of database "ABC" has not been...
0
by: vaibhavsumant | last post by:
<project name="DBCreate" default="usage" basedir="."> <property name="user" value="db2admin"/> <property name="passwd" value="db2admin"/> <property name="dbprefix" value=""/> <property...
2
by: elgin | last post by:
I have a split Access 2003 database. I have signed the database with a Code Signing Certificate from Small Business Server. This works fine and users can have Access macro security on high or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.