By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

Trouble with maps ( i.e., map <char*, map<char*, char*> > )

jlm699
100+
P: 314
I've looked at the other articles about maps of maps and am still stuck on this!
I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain variables I'd like to display a state. So I wanted to use maps for quick look-ups... anyway here's my code for filling and verifying the contents of my map of maps:

Expand|Select|Wrap|Line Numbers
  1. include<map>
  2. #define MAX_LENGTH 80;
  3.  
  4. void gen_enums(void)
  5. {
  6.     char varname[MAX_LENGTH] = "init", varval[MAX_LENGTH], line[MAX_LENGTH];
  7.     char temp[MAX_LENGTH], varkey[MAX_LENGTH];
  8.     char *pch;
  9.     map<char*, map<char*, char*> > enum_map;
  10.     map<char*, char*> temp_map;
  11.     FILE *enum_file, *test_file;
  12.  
  13.     enum_file = fopen("enums.txt", "r");
  14.     if(enum_file == NULL)
  15.     {
  16.         xmdprintf("Error::No enumeration file found to load");
  17.         return;
  18.     }
  19.     test_file = fopen("enum_test.txt", "w");
  20.  
  21.     /* Next, begin parsing the lines in the file looking for variable names */
  22.     while(fgets(line, MAX_NAME_LENGTH, enum_file) != NULL)
  23.     {
  24.         sscanf(line, "%s", varname);
  25.  
  26.         fprintf(test_file, "Read variable %s\n", varname);
  27.         fflush(test_file);
  28.         /* If line has '=' it is a key, value pair */
  29.         pch = strrchr (varname, '=');
  30.         if (pch == NULL)
  31.         {
  32.             while(fgets(temp, MAX_NAME_LENGTH, enum_file) != NULL)
  33.             {
  34.                 if (!strcmp(temp, "\n") || !strcmp(temp, "")) { break; }
  35.                 fflush(test_file);
  36.                 int test_values;
  37.                 sscanf(temp, "%d %*c %s", &test_values, varkey);
  38.                 sprintf(varval, "%d", test_values);
  39.                 fflush(test_file);
  40.                 temp_map[varval] = varkey;
  41.                 map<char*, char*>::iterator test_iter;
  42.                 test_iter = temp_map.find(varval);
  43.                 if (test_iter == temp_map.end()) fprintf(test_file, "%s failed\n", varval);
  44.                 else fprintf(test_file, "Insert %s -> %s success\n", test_iter->first, test_iter->second);
  45.             }
  46.             map<char*, char*>::iterator iter;
  47.             for (iter = temp_map.begin(); iter != temp_map.end(); iter++)
  48.             { 
  49.                 fprintf(test_file, "Map: %s -> %s\n", iter->first, iter->second);
  50.             }
  51.             enum_map[varname] = temp_map;
  52.         }
  53.         map<char*, map<char*, char*> >::iterator iter_o;
  54.         map<char*, char*>::iterator iter_i;
  55.         iter_o = enum_map.find(varname);
  56.         if (iter_o == enum_map.end()) fprintf(test_file, "%s not found in outer map\n", varname);
  57.         else
  58.         {
  59.             iter_i = iter_o->second.find("1");
  60.             if (iter_i == iter_o->second.end()) fprintf(test_file, "1 not found in inner map\n");
  61.             else fprintf(test_file, "Testing enum_map w/ %s: %s -> %s\n", iter_o->first, iter_i->first, iter_i->second);
  62.         }
  63.     }
  64.  
  65.     fclose(enum_file);
  66.     fclose(test_file);
  67. }
Here's an example of the contents in the enum_file:
S_Dir_Call
0=Cnt
1=Fwd
2=Rev
3=Invld

S_State
0=Stop
1=Start
2=Err
3=Foo
4=Bar

Now when I view the enums_test.txt it looks like this:
Read variable S_Dir_Call
Insert 0 -> Cnt success
Insert 1 -> Fwd success
Insert 2 -> Rev success
Insert 3 -> Invld success
Map: 3 -> Invld
1 not found in inner map
Read variable S_State
Insert 0 -> Stop success
Insert 1 -> Start success
Insert 2 -> Err success
Insert 3 -> Foo success
Insert 4 -> Bar success
Map: 4 -> Bar
1 not found in inner map


So I have two problems... the first is that the iterator isn't iterating and the second is that my outer map isn't getting it's values!
Any help would be greatly appreciated!!
Jan 16 '08 #1
Share this Question
Share on Google+
4 Replies


jlm699
100+
P: 314
Ok so I added a size() check after each temp_map[varval] = varkey and the size is always 1. This is probably why I can't access the "1" element because somehow it's being over-written by the last element that was added to the map...

I'll look into it further...
Jan 16 '08 #2

jlm699
100+
P: 314
Well I gave up on using c_strings and modified my maps for strings. Whenever I need to make use of c strings I just convert the string using c_str and use a strcpy... Problem solved.
Jan 16 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Your code won't even compile.

First problem is right here:
#define MAX_LENGTH 80;
Second problem is:
void gen_enums(void)
{
etc...
This function as no input and no output. That means your entire application must a) be a global object or b) be inside this function.

Third problem is using C++ in a C program. Some C constructs won't compile in C++ or mean something different.

Fourth problem is an unclear statement of your data design. Since I have idea what you are trying to do, I can't determine if your map is set up correctly.
Jan 16 '08 #4

jlm699
100+
P: 314
Yeah, sorry I took an excerpt from a large program to try and demonstrate my problem. But didn't actually check it for errors.

No worries however as I've solved the problem by switching from char* to string for the map storage...
Jan 17 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.