要读取ini文件,首先读取文件夹下所有ini文件名,这些文件名是字母数字组成的
如camera1.ini, camera101.ini, camera2.ini
要排下序,变为camera1.ini, camera2.ini, camera101.ini
备忘下,基本都是参考各方大佬的代码拼凑而成,感谢!
BOOL CamDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
char appPath[512] = { '\0' };
GetModuleFileName(NULL, appPath, 512);
char* fileDir = strrchr(appPath, '\\');
if (fileDir)
{
*fileDir = '\0';
}
char fileFolderPath[512] = { '\0' };
sprintf_s(fileFolderPath, "%s\\CameraConfig\\", appPath);
char filePath[512] = { '\0' };
sprintf_s(filePath, "%s*.ini", fileFolderPath);
CFileFind find;
BOOL IsFind = find.FindFile(filePath);
std::vector<CString> fileList;
CString fileName;
while (IsFind)
{
IsFind = find.FindNextFile();
if (find.IsDots())
{
continue;
}
else
{
fileName = find.GetFileName();
fileList.push_back(fileName);
}
}
SortTypeName(fileList);
return TRUE;
}
void CamDlg::SortTypeName(std::vector<CString>& fileVect)
{
std::vector<std::pair<int, CString>> vecPair;
for (int i = 0; i < fileVect.size(); i++)
{
CString strName = fileVect[i].Left( fileVect[i].ReverseFind('.') ); //Camera1
int n = 0;
for (int j = strName.GetLength(); j > 0; j--)
{
char p = strName[j];
if (p == _T('a'))
{
n = j;
break;
}
}
int numRight = _ttoi(strName.Right(strName.GetLength() - 1 - n)); //Right返回字符串后nCount个字符的字符串
std::pair<int, CString> pairName(numRight, fileVect[i]);
vecPair.push_back(pairName);
}
fileVect.clear();
for (int k = 0; k < vecPair.size(); k++)
{
for (int t = 0; t < vecPair.size() - k - 1; t++)
{
if (vecPair[t].first > vecPair[t+1].first)
{
std::swap(vecPair[t], vecPair[t + 1]);
}
}
}
for (int m = 0; m < vecPair.size(); m++)
{
fileVect.push_back(vecPair[m].second);
}
}