A
Andrew Fabbro
I think FAQ 2.15 ("How can I access structure fields by name at run
time?") answers my question, but I don't like the answer
I have a struct like this:
struct config_struct {
char file_dir[MAXPATHLEN];
char debug_dir[MAXPATHLEN];
/* and many more */
} config;
I have a text file like this:
file_dir /usr/local/blah/file/
debug_dir /usr/local/blah/debug
Parsing this is easy, but populating the config struct is not. Well,
it's easy but not fun. What I want is something like (pseudocode):
read in option_name, option_value
config.(option_name) = option_value (be it by strcpy or whatever)
It appears there's no way to do this in C. I've looked at some code
written by far wiser minds than I, and they do a lot of
if ( strcmp(option_name, "file_dir") == 0 )
strcpy(config.file_dir, option_value);
and such. I suppose I could keep an array of field names and their
sizeofs and then jump around with a pointer...is that really the best
way?
-Drew
time?") answers my question, but I don't like the answer
I have a struct like this:
struct config_struct {
char file_dir[MAXPATHLEN];
char debug_dir[MAXPATHLEN];
/* and many more */
} config;
I have a text file like this:
file_dir /usr/local/blah/file/
debug_dir /usr/local/blah/debug
Parsing this is easy, but populating the config struct is not. Well,
it's easy but not fun. What I want is something like (pseudocode):
read in option_name, option_value
config.(option_name) = option_value (be it by strcpy or whatever)
It appears there's no way to do this in C. I've looked at some code
written by far wiser minds than I, and they do a lot of
if ( strcmp(option_name, "file_dir") == 0 )
strcpy(config.file_dir, option_value);
and such. I suppose I could keep an array of field names and their
sizeofs and then jump around with a pointer...is that really the best
way?
-Drew