By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,171 Members | 1,163 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,171 IT Pros & Developers. It's quick & easy.

Structure and ASCII "Database"

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
> 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

P: n/a
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

P: n/a

"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

P: n/a

- 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

P: n/a
"- 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

P: n/a
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

P: n/a
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

P: n/a

"- 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

P: n/a
"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

P: n/a


- 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

P: n/a


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

P: n/a
"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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.