"jaso" <> wrote in message
>
> If have a structure of a database record like this:
> struct record {
> char id[ID_LENGTH];
> char title[TITLE_LENGTH];
> ...
> };
> Is there some way to find out how many member variables there is
> in the struct and then iterate through them?
> I want to have a function that takes the struct as argument and
> then prompts the user for input for all the members. And instead
> of coding the input for every member variable, I want the function
> to automatically update each member. That will make it easier to
> update the struct with more members.
> The function also needs to know the length of every string in the
> struct, so maybe I should add a length variable to each string
> in the struct.
>
> Is that feasible? Or can you recommend an other way?
>
No.
If we've got a database of employees, we can hardcode something like
struct employee
{
char name[64];
int serialnumber;
double salary;
};
Unfortunately in a real application we probably want the user to be able to
add and delete fields, without recompiling the program.
There is no easy way of achieving this. If you look at SQL you will see that
there are a limited number of atomic data types. You can write an SQL-type
server by defining a record signature string. Then you query the fields by
name and extract the values.
So we've got something like
"id: char[32]
title: varchar
... other members
"
In our record format descriptor
Then we have
struct record
{
int Nfields;
char **fieldname;
int *fieldtype;
int *fieldlength;
void **data;
};
for the general record, and we can build the structure from the record
descriptor
then we have an
int extractcharfield(struct record *rec, char *fieldname, char *out)
to access the data.
It is quite complicated to build from the ground up.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available
www.lulu.com/bgy1mm