Structure and ASCII "Database"

S

- Steve -

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?
 
N

Nathan

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
 
N

Nick Austin

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.
 
S

- Steve -

Nick Austin said:
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.


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?
 
M

Mike Wahler

- Steve - said:
Nick Austin said:
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.


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
 
I

Irrwahn Grausewitz

- Steve - said:
#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
 
E

Emmanuel Delahaye

- Steve - said:
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.
 
E

Emmanuel Delahaye

- Steve - said:
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.
 
M

Malcolm

- Steve - said:
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, 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.
 
A

ArWeGod

Malcolm said:
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, 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.
 
A

Al Bowers

- Steve - said:
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];
}
 
R

richard

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.

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];
}
 
A

Al Bowers

richard said:
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.
 
A

ArWeGod

Al Bowers said:
- Steve - said:
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: (e-mail address removed) (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.
 
A

Al Bowers

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.
 
R

richard

richard wrote:

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

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.
agreed

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.

I guess there's (sizeof *tmp)*(p->total+1).

Agree that it's a matter of preference
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top