Thread: From config file to data structure

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    From config file to data structure

    I need to read a configuration file and store its options in a data structure. For example:

    Code:
    struct ConfigurationFile{
    
    int option1;
    int option2;
    double option3;
    
    };
    And the configuration file would be:

    #begin of file
    this is an option = value of this option
    another option = some value1, some value2, ...
    ...
    last option = final value
    #end of file

    What I have done so far is not even good. I have created three structures, one for each kind of option: integer, float or array of floats. Each option has a string that represent it. Three array (one of each kind of option) are created. After reading an option I iterate through the arrays and look for the one that has the string. For example:

    Code:
    struct OpcaoInteira{
        char opcao[50];
        int valor;
    };
    
    struct OpcaoReal{
        char opcao[50];
        double valor;
    };
    
    struct OpcaoVetor{
        char opcao[50];
        double* valores;
    };
    
    
    OpcaoInteira opInteiras[] = {
        {"numero aneis", 0},
        {"numero aneis concentricos", 0}
    };
    
    
    OpcaoReal opReais[] = {
        {"drmax", 350.e-6},
        {"posicao eletrons", 200.0}
    };
    
    OpcaoVetor opVetores[] = {
        {"ra", NULL},
        {"z0", NULL}
    };
    The problem is not to read the options and values from the file, but to store the options in the ConfigurationFile structure. There are many possible options, and a "if-else" approach would not suffice. Please, if I not explained correctly my problem I would be happy to give more details.

    Any help is much appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you might try a union. With this there is only one structure, and you can easily extend it to include other data types. You can also replace that opcao string with a numberic integer that indicates the type of data stored in the union (1 = int, 2 = double, 3 = double array, etc)
    Code:
    struct OpcaoInteira{
        char opcao[50];
        union
        {
             int       ival;
             double dval;
             double* darray;
        }
    };

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Thanks! Everything is ready now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. Data from file (XML, Config, ..) to Object
    By WMonk in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2008, 05:59 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM