Jump to content
  • Advertisement


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


What am I doing wrong with the map?

This topic is 5653 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
    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');

        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));
        { // 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


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

    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
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
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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!