今天用到读取文件夹里的jpg文件名,然后去招了一下c++的,下面是代码,我真想吐槽一下鳌,python就一句listdir的问题,c++居然要这么复杂。
还有要注意在64位系统下,句柄hFile 要用_int64,不然会报错,其他位系统用long。
//读取文件名
void GetAllFiles(string path, vector<string>& files)
{
_int64 hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*.jpg").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
files.push_back(fileinfo.name);
cout << fileinfo.name;
}
}while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}