如图,我是以时间戳为区分依据,将时间戳相同的图片分别做统计
map<CString, int>DiretlyStastic;//存放时间戳以及该时间戳下的图片数量
void CFileSeparationDlg::OnBnClickedDirectlystastic()
{
CString text;
CFile file;
GetDlgItemText(IDC_EDIT1, text);
USES_CONVERSION;
char* fileName = T2A(text);//读取需要分离的图片的路径CString->char*
std::vector <CString> imgNames;//用来保存图片的名字
readImgNamefromFile(fileName, imgNames);//读入图片名字到数组
CString str;
int Length = imgNames.size();//获取图片名字数组长度
string picture;//图片名字数组CString->string
int num=0;//计数每一秒图片数量
for (int i = 0; i < Length; i++)
{
picture = CT2A(imgNames[i].GetString());//图片名字数组CString->string
//int j = imgNames[i].GetLength();//每一个图片名字的长度
//str = imgNames[i].Mid(0, j - 9);//去掉图片名字后缀
int r = imgNames[i].Find(_T("#"));
str = imgNames[i].Mid(0, r - 8);//去掉图片名字后缀
int nRet = std::count(Second.begin(), Second.end(), str);
if (nRet == 0)
{
Second.push_back(str);
num++;
}
else
{
num++;
}
subFileNum.push_back(num);
if (DiretlyStastic.find(str) == DiretlyStastic.end())//map容器中是否有该key值
{
DiretlyStastic.insert(make_pair(str,num));
num = 0;
}
}
file.Open(_T("D:\\PictureNum.txt"), CFile::modeCreate | CFile::modeReadWrite);
map<CString, int>::iterator iter;
CString sec;
for (iter = DiretlyStastic.begin(); iter != DiretlyStastic.end(); iter++)
{
sec.Format(_T("%d"), iter->second);
WORD unicode = 0xFEFF; //这句重要,注意这里是F E FF,中间有个E
file.Write(&unicode, 2); //这句重要
file.Write(iter->first, wcslen(iter->first) * sizeof(wchar_t));
file.Write(_T("的图片数量为:"), wcslen(_T("的图片数量为:")) * sizeof(wchar_t)); //这句重要
file.Write(sec, wcslen(sec) * sizeof(wchar_t)); //这句重要
file.Write(_T("\n"), 2);
}
file.Close();
MessageBox(_T("统计完成,请查看D:PictureNum.txt"));
}
void readImgNamefromFile(char* fileName, vector <CString> &imgNames)
{
// vector清零 参数设置
imgNames.clear();
WIN32_FIND_DATA file;
int i = 0;
char tempFilePath[MAX_PATH + 1];
char tempFileName[50];
// 转换输入文件名
sprintf_s(tempFilePath, "%s/*", fileName);
// 多字节转换
WCHAR wstr[MAX_PATH] = { 0 };
MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr));
// 查找该文件待操作文件的相关属性读取到WIN32_FIND_DATA
HANDLE handle = FindFirstFile(wstr, &file);
if (handle != INVALID_HANDLE_VALUE)
{
FindNextFile(handle, &file);
FindNextFile(handle, &file);
// 循环遍历得到文件夹的所有文件名
do
{
sprintf_s(tempFileName, "%s", fileName);
imgNames.push_back((file.cFileName));
// imgNames[i].Insert(0, 1);
i++;
} while (FindNextFile(handle, &file));
}
FindClose(handle);
}
结果展示: