VC++ 如何读取一个目录下的所有指定格式的文件
VC++ 怎么读取一个目录下的所有指定格式的文件?
VC++ 怎么读取一个目录下的所有指定格式的文件?
例如:目录为C:\Music
这个目录下有.mp3,.wma格式的文件,还有其他格式的文件如:.txt等
我想把这个目录下的所有.mp3,.wma文件都读取出来放在一个数组里,
怎么读取!
求助!!!
------解决方案--------------------
CFileFind::FindFile/FileNextFile()遍历,得到文件路径,然后比对后缀名
------解决方案--------------------
_splitpath
------解决方案--------------------
是的,用CFileFind类的方法,首先获取一个文件路径[code=C/C++][/code] void AddFolder(CString csFolder)
{
if (csFolder[csFolder.GetLength() - 1] != '\\') csFolder += _T("\\");
csFolder += _T("*.*");
CFileFind ff;
BOOL bFind = ff.FindFile(csFolder);
while (bFind)
{
bFind = ff.FindNextFile();
if (ff.IsDots()) continue;
if (ff.IsDirectory())
{
AddFolder(ff.GetFilePath());
continue;
}
CString csFile = ff.GetFilePath();
csFile.MakeLower();
int nFileType = GetFileType(csFile);
if (nFileType == -1) continue;
}
} 其中GetFileType是自定义的函数,里边设置相关的文件格式,基本上思路就是这样,你可以参考一下,代码有些细节可能不是很清楚,但总体思想就是这样
------解决方案--------------------
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.mp3"); // 遍历mp3后缀文件
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// get infomation of file
CString str = finder.GetFilePath();
}
finder.Close();
------解决方案--------------------
CString strWildcard(pstr); // pstr 目录名
------解决方案--------------------
------解决方案--------------------
HANDLE hf = FindFirstFile(strFindPath, &wfd);
if (INVALID_HANDLE_VALUE != hf)
{
while (FindNextFile(hf, &wfd))
{
}
FindClose(hf);
}
------解决方案--------------------
查找
比较后缀名
------解决方案--------------------
循环目录下 每一个文件,判断 文件 后缀名 是否 mp3, wma
然后做相应操作。注意大小写。
如楼上的大哥们说的 用
CFileFind 类
finder.GetFilePath(); 获取到文件的全路径名
然后使用rfind 发现最后一个"."的位置,截取从此位置+1后的字串和 mp3, wma
------解决方案--------------------
我有一源码, 要否?
大二写的
------解决方案--------------------
VC++ 怎么读取一个目录下的所有指定格式的文件?
例如:目录为C:\Music
这个目录下有.mp3,.wma格式的文件,还有其他格式的文件如:.txt等
我想把这个目录下的所有.mp3,.wma文件都读取出来放在一个数组里,
怎么读取!
求助!!!
------解决方案--------------------
CFileFind::FindFile/FileNextFile()遍历,得到文件路径,然后比对后缀名
------解决方案--------------------
_splitpath
------解决方案--------------------
是的,用CFileFind类的方法,首先获取一个文件路径[code=C/C++][/code] void AddFolder(CString csFolder)
{
if (csFolder[csFolder.GetLength() - 1] != '\\') csFolder += _T("\\");
csFolder += _T("*.*");
CFileFind ff;
BOOL bFind = ff.FindFile(csFolder);
while (bFind)
{
bFind = ff.FindNextFile();
if (ff.IsDots()) continue;
if (ff.IsDirectory())
{
AddFolder(ff.GetFilePath());
continue;
}
CString csFile = ff.GetFilePath();
csFile.MakeLower();
int nFileType = GetFileType(csFile);
if (nFileType == -1) continue;
}
} 其中GetFileType是自定义的函数,里边设置相关的文件格式,基本上思路就是这样,你可以参考一下,代码有些细节可能不是很清楚,但总体思想就是这样
------解决方案--------------------
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.mp3"); // 遍历mp3后缀文件
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// get infomation of file
CString str = finder.GetFilePath();
}
finder.Close();
------解决方案--------------------
CString strWildcard(pstr); // pstr 目录名
------解决方案--------------------
------解决方案--------------------
HANDLE hf = FindFirstFile(strFindPath, &wfd);
if (INVALID_HANDLE_VALUE != hf)
{
while (FindNextFile(hf, &wfd))
{
}
FindClose(hf);
}
------解决方案--------------------
查找
比较后缀名
------解决方案--------------------
循环目录下 每一个文件,判断 文件 后缀名 是否 mp3, wma
然后做相应操作。注意大小写。
如楼上的大哥们说的 用
CFileFind 类
finder.GetFilePath(); 获取到文件的全路径名
然后使用rfind 发现最后一个"."的位置,截取从此位置+1后的字串和 mp3, wma
------解决方案--------------------
我有一源码, 要否?
大二写的
------解决方案--------------------
- C/C++ code
//查找文件 bool findFile(const string name) { //记录当前路径 string path = getcwd(NULL, 0); string p = path; path += '\\'; p += '\\'; //查找文件并执行相关操作 //句柄 long handle = -1; //文件信息 struct _finddata_t fileinfo; //查找第一个文件信息 handle = _findfirst(name.c_str(), &fileinfo); //第一个文件查找失败 if (handle == -1) { return false; } //记录文件路径和名字 path += fileinfo.name; //执行其它相关操作 dosome(path); path = p; //查找其它文件 while (_findnext(handle, &fileinfo) != -1) { path = p; path += fileinfo.name; dosome(path); } //关闭句柄 _findclose(handle); return true; }
------解决方案--------------------