Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sand_Hawk

What am I doing wrong with the map?

This topic is 5736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working with a map to map a file to memory. However, when I add something to the map manually(By code) it works, but when I load from file, it totally goes wrong. This is my code for loading from file
    
    Sections.clear();
    FileIn.open(szFilename, ios::in);
    if (!FileIn.is_open())
    {
        return (CFG_FILENOTFOUND);
    }

    Sections["a"].SetVariable("b", "c");
    // Read entire file

    while (!FileIn.eof())
    {
        FileIn.getline(szLine, 512, '\n');
        Trim(szLine);
        StringToLower(szLine);

        if (szLine[0] == '[')
        { // We are dealing with a new section here

            if (strlen(szLine) > 2)
                strncpy(szSection, szLine + sizeof(char), strlen(szLine) - 2 * sizeof(char));
        }
        else
        { // This is a normal variable

            if (strlen(szLine) > 0)
            {
                // Tokenize the string

                szToken = strtok(szLine, "=");
                if (szToken != NULL)
                {
                    strcpy(szKey, szToken);
                    szToken = strtok(NULL, "");
                    strcpy(szValue, szToken);

                    // Remove unneeded spaces

                    Trim(szKey);
                    Trim(szValue);

                    Sections[szSection].SetVariable(szKey, szValue);
                }
            }
        }
    }

    FileIn.close();
    return (CFG_SUCCESS);
}
  


The 2 maps look like this:

      
struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

map<const char*, const char*, ltstr> Variables;
map<const char *, CSection, ltstr> Sections;
    
I am stuck, I don't know what I am doing wrong. Sand Hawk [edited by - sand_hawk on April 2, 2003 7:42:20 AM]

Share this post


Link to post
Share on other sites
Advertisement
szLine, szSection and szKey are pointers that won't change. Using a pointer as an index into a map won't use its contents. It won't automagically duplicate the string it points to. Use std::string instead. That'll fix it.

Scheldwoorden zijn trouwens onnodig in testcode.



[edited by - Kippesoep on April 2, 2003 6:52:26 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sand_Hawk
lol Kippesoep, I was very pissed at that point I removed them

Aw man, I''m missing all the fun down here...

Share this post


Link to post
Share on other sites
Yup, you missed you the fun :-). However, the most fun part is that I fixed the problems. The first problem, where it was having garbage, was caused by some bad pointers:


char *szKey;
char *szValue;
char *szSection;


Kinda stupid when you don''t do a new . The second problem I was having(No data) was solved by using std::string. So everything works now, thanks alot.

Sand Hawk

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!