I have to map containers:
map<string, char *> mOServs;
map<LPWCH, int> mymap;
I do a sqlite3 query and try to update these two maps with the results:
sprintf_s(query,1024,"SELECT oservname FROM OServs;");
rc = sqlite3_get_table(db, query, &results, &nrow, &ncol, &zErrMsg);
cout << "query: " << query << endl;
if (rc != SQLITE_OK) {
fprintf(stderr, "Error Selecting oserv name: %s\n", zErrMsg);
sqlite3_free_table(results);
sqlite3_close(db);
return -1;
}
int X = 0;
WCHAR somevar[1024];
while(X < nrow)
{
mbstowcs(somevar, results[X+1], 1024 );
wcstombs(output, somevar, 1024);
mOServs[output] = "offline";
mymap[somevar] = X;
X++;
}
Somehow mOServs seems to populate properly, but with mymap, it only contains the last record of the query at the end of the loop. Am I doing something wrong?
I am trying to store all the query results in a map, and so that, I can do a mymap.find(LPWCH), to find if a record is in the table or not.
Perhaps you meant to do something more like this:
Edit: Actually why do you need a map here at all?
std::vectorwould probably suffice it doesn’t make much sense to have either the key or the value of the map be set to a temporary loop count value.Edit#2: Actually I just realized something else crazy is happening. First you convert from
char*towchar_t*then on the very next line you convert it right back fromwchar_t*tochar*.This is unnecessary. Decide if you want to use
std::stringorstd::wstringand prefer these objects. Mixing and matching character arrays, with pointers to arrays, with typedefs of pointers to array, with string objects, and also mixing wide character and multibyte characters is just asking for trouble.