I'm kind of a noob with the std namespace, and I'm writing code that loops through all the jpeg files in a directory and removes any exclamation points. I'm trying to use std::string and std::vector. My problem is that my variable tempname: const char tempname = (char) *filelist[j].c_str(); changes as the strings in the vector filelist changes (which it shouldn't - it is a constant variable. Here is the meat of my WinMain function:
std::vector<std::string> filelist;
if (!dirExists(directory)) //checks if a directory exists
{
CreateDirectory("resized", NULL);
}
std::vector<std::string> filelist = findFiles("*.jpg"); //finds files in its directory with a given extension
int result; //for rename function
for (unsigned int j=0; j< filelist.size(); j++)
{
std::string::size_type pos = filelist[j].find("!"); //check for exclamation points
if (std::string::npos != pos) //found one at index "pos" in the string
{
switch (MessageBox(window, (LPCSTR)filelist[j].c_str(), "Illegal filename - Rename?", MB_YESNO)) //user input
{
case IDYES:
{
const char tempname = (char) *filelist[j].c_str(); //the problem
//attempt to remove the exclamation point
result = rename(&tempname, filelist[j].erase(pos, 1).c_str());
if (result == 0)
MessageBox(window, "Renamed File", "Information", MB_OK);
else
MessageBox(window, "Error renaming file", "Error", MB_OK);
break;
}
case IDNO:
{
break;
}
}
}
}
Assuming the filename contains no more than one exclamation point. If I defined tempname as a const char* this would make sense, because it would be a pointer - tempname's value could change without violating the const declaration if the data it pointed to changed. But taking away the pointer, I'm baffled.