私はstd
名前空間の初心者のようなもので、ディレクトリ内のすべての jpeg ファイルをループして感嘆符を削除するコードを書いています。と を使おうとしていstd::string
ますstd::vector
。私の問題は、変数 tempname:const char tempname = (char) *filelist[j].c_str();
が、ベクトル内の文字列が変更されると変更されるfilelist
ことです (これは、定数変数であってはなりません。これが私の WinMain 関数の内容です。
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;
}
}
}
}
ファイル名に含まれる感嘆符は 1 つだけであると仮定します。tempname を this として定義した場合const char*
、それはポインターになるため、意味があります。tempname の値はconst
、それが指すデータが変更された場合、宣言に違反することなく変更できます。しかし、ポインタを取り除いて、私は困惑しています。