I am experiencing some unexpected behavior when trying to use map to store some filenames given a directory. My code looks like this:
The directory I am passing has 361 converted (to pnm) dicom files with a naming format IM-####-####.pnm. I've added some cout statements to understand why my 0001 key is changing values from IM-0001-0001.pnm to 0103.pnm after file IM-0001-0101.pnm is inserted into the files map container.Code:typedef map<string, char*> MapType; int getFilesFromDir(char* directory, MapType &files) { DIR *dp; struct dirent *dirp; regex_t re; if ((dp = opendir(directory)) == NULL) { cout << "Error(" << errno << ") opening input directory: " << directory << endl; return errno; } while ( (dirp=readdir(dp))!=NULL ) { if ( regcomp(&re, "^IM[-][[:digit:]]{4}[-][[:digit:]]{4}[.][Pp][Nn][Mm]$", REG_EXTENDED|REG_NOSUB)!=0 ) continue; else { int status = regexec(&re, dirp->d_name, (size_t)0, NULL, 0); regfree(&re); if (status != 0) { cout << "regex condition not satisfied......" << dirp->d_name << endl; continue; } else { string key; key = dirp->d_name[8]; key += dirp->d_name[9]; key += dirp->d_name[10]; key += dirp->d_name[11]; key += '\0'; //files[key] = (char*)(dirp->d_name); files.insert(std::pair<string, char*>(key, (char*)dirp->d_name)); cout << key << ": " << files[key] << endl; key = '0'; key += '0'; key += '0'; key += '1'; key += '\0'; cout << key << ": " << files[key] << endl; } } } char buffer [4]; for (unsigned int i=1; i<= files.size(); i++) { string key; sprintf(buffer, "%.4i", i); key = buffer; key += '\0'; cout << key << ": " << files[key] << endl; } closedir(dp); return 0; }



LinkBack URL
About LinkBacks


