from a text file to a structure ?

This is a discussion on from a text file to a structure ? within the C Programming forums, part of the General Programming Boards category; So far i have written a program that reads a config file which is written in the form of: Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Unhappy from a text file to a structure ?

    So far i have written a program that reads a config file which is written in the form of:
    Code:
    // settings.cfg
    // comment
    item "value1 value2"
    item2 value
    The program assignes the the following:
    item to char szVar[20]
    values to char szVal[20] - (szVal is everything between the quotes or the 1st value if there arnt any quotes)

    Now for the second part of the program i would like to assign each value to a value in a structure. the variables in the structure have the same name as the item in the config file. this is what my structure looks like at the moment:
    Code:
    typedef struct _SETTINGS {
      string          item;
      string          item2;
    } SETTINGS;
    i could do it this way:
    Code:
    if (szVar == item) {
      // assigne value to settings.item
    }
    else if (szVar == item2) {
      // assigne value to settings.item2
    }
    But it looks too messy when you have about 10-15 items and i dont think its very good practice. i tried using a switch case statment but then realised that they only take intagers
    Is there a way that you can make a variable name the value of szVar
    say.. szVar = item; can i make a variable with the name of what is held in szVar which is item. this is the only other way i can think of doing this.
    if anyone can suggest anyother way to do this that would be great.

    Thanks for taking your time to help me out

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    if you are using c++, stl map is perfect for you. It lets you assign values to keys. Here is a small example.

    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	map<string,string> settings;
    	
    	settings["fullscreen"]="yes";
    	settings["invert mouse"]="no"; 
    
    	map<string,string>::const_iterator i;
    	
    	for(i=settings.begin();i!=settings.end();i++)
    		cout<<i->first<<"="<<i->second<<endl;
    	return 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if you are using c++
    Although the code in the OPs post does reference a "string" object, we'll have to assume for now that this is a C program, as they've posted on the C forum. Maybe the OP can clarify?!

    Anyways, here's one way to achieve the goal. Normally I wouldn't give you a complete code answer, but I think it'll be easy than trying to explain what I'm thinking!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct Option
    {
      const char  *Name;
      int   Value;
    };
    
    int FindOptionIndex(struct Option *OptionsTable, int Length, const char *Criteria)
    {
      int i;
      
      for (i = 0; i < Length; i++)
      {
        if (strcmp(OptionsTable[i].Name, Criteria) == 0)
          return i;
      }
      printf ("Invalid option: >%s<\n", Criteria);  
      return -1;
    }
    
    int main(void)
    {
      struct Option myOptions[2] = {
        {"item1", -1},
        {"item2", -1}
      };
      char SimulatedInput[] = "item2";
      int Index;
      int i;
      
      if ((Index = FindOptionIndex(myOptions, sizeof(myOptions)/sizeof(*myOptions), SimulatedInput)) != -1)
      {
        myOptions[Index].Value = 99;
      }
      
      for (i = 0; i < sizeof(myOptions)/sizeof(*myOptions); i++)
      {
        printf ("%s = %d\n", myOptions[i].Name, myOptions[i].Value);
      }
      
        
      return(0);
    }
    The "struct Option" contains the information for a single option, "myOptions" is an array of these. In main, we initialise the array to have names and default values.

    The FindOptionIndex() function searches the array for an entry with a Name element that matches the input criteria. If no match is found, -1 is returned. The code in main() calls this function to determine the index location of a particular option, then sets the appropriate value, in this case to a hard coded value of 99.

    In case you don't recognise it, this piece of code:
    sizeof(myOptions)/sizeof(*myOptions)
    ... simply works out the count of elements in the myOptions array, which equates to 2 in this code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    thanks a lot for the help Hammer i am at the moment trying to implement it into the program now. Im just trying to understand how this code works as im still fairly new to the C language but if i have ny more trouble i know where to ask. thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21