intptr_t类型用于记录文件夹句柄,注意该类型不是指针类型,而是int型的重定义。

_finddata_t结构体类型用于记录文件信息。

_finddata_t结构体定义如下

struct _finddata_t {

unsigned attrib;        // 存储文件属性
__time64_t time_create;     // 存储文件创建时间
__time64_t time_access;     // 存储文件最后一次被访问的时间
__time64_t time_write;      // 存储文件最后一次被修改的时间
_fsize_t size;          // 存储文件的大小
char name[260];        // 存储文件名称

};

_findfirst()函数

_findfirst()函数原型如下:

intptr_t _findfirst(

  const char *filespec,      // 目标文件

  struct _finddata_t *fileinfo    // 用于存储文件信息的_finddata_t结构体

);

函数如果成功,返回一个唯一的搜索句柄标识一个或一组和filespec说明匹配的文件,可以用于接下来的_findnext()和_findclose()函数。
否则_findfirst()返回-1。 _findnext()函数
_findnext()函数原型如下: int _findnext(
    intptr_t handle,            // 搜索句柄,通过_findfirst()函数获得
struct _finddata_t *fileinfo     // 用于存储文件信息的_finddata_t结构体
);
函数如果成功,返回0,否则返回-1。如果没有更多能够找到的文件了,也会导致失败。
_findclose()函数
原型如下:
int _findclose(
intptr_t handle    // 搜索句柄
);
该函数用于关闭搜索句柄 代码如下:
void CDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_ListFile.ResetContent(); // 宽字节转多字节
char *pPathBuf = NULL;
int PathBufSize = WideCharToMultiByte(0, 0, m_szPath.GetBuffer(), m_szPath.GetLength(), pPathBuf, 0, NULL, NULL);
if (PathBufSize <= 0)
m_ListFile.AddString(_T("获取多字节缓冲区大小错误"));
pPathBuf = new char[PathBufSize + 1];
memset(pPathBuf, 0, PathBufSize + 1);
WideCharToMultiByte(0, 0, m_szPath.GetBuffer(), m_szPath.GetLength(), pPathBuf, PathBufSize + 1, 0, 0);
if (strlen(pPathBuf) <= 0)
m_ListFile.AddString(_T("宽字节转多字节错误")); queue<string> *pVect = new queue<string>;
if (GetPathFile(pPathBuf, pVect) == false)
m_ListFile.AddString(_T("遍历目录下的所有文件失败!"));
else{
while (!pVect->empty())
{
string szFileName = pVect->front();
LPWSTR pBuf = NULL;
int nLen = MultiByteToWideChar(0, 0, (char*)szFileName.c_str(), szFileName.length(), pBuf, 0);
if (nLen > 0)
{
pBuf = new TCHAR[nLen + 1];
memset(pBuf, 0, sizeof(TCHAR)* (nLen + 1));
MultiByteToWideChar(0, 0, (char*)szFileName.c_str(), szFileName.length(), pBuf, nLen);
m_ListFile.AddString(pBuf);
delete[] pBuf;
pBuf = NULL;
}
pVect->pop();
}
} delete[] pPathBuf;
pPathBuf = NULL;
UpdateData(FALSE);
} bool CDlg::GetPathFile(const char* pPath, queue<string> *pVect)
{
if (!pPath || !pPath)
return false;
char* szPath = new char[128];
memset(szPath, 0, 128);
_snprintf_s(szPath, 128, 128, "%s\\*.*", pPath);
intptr_t Handle;
_finddata_t FindData;
Handle = _findfirst(szPath, &FindData);
if (Handle == -1)
return false;
do
{
if (strcmp(FindData.name, ".") != 0 && strcmp(FindData.name, "..") != 0)
{
pVect->push(FindData.name);
if (strrchr(FindData.name, '.') == NULL)
{
string sz = pPath;
sz += "\\";
sz += FindData.name;
GetPathFile(sz.c_str(), pVect);
}
}
} while (_findnext(Handle, &FindData) == 0);
_findclose(Handle);
delete[] szPath;
szPath = NULL;
return true;
}

  

												

最新文章

  1. java 多线程之wait(),notify,notifyAll(),yield()
  2. Radmin Server-3.5 完美绿色破解版(x32 x64通用) 第三版 + 单文件制作方法
  3. Castle.ActiveRecord 多对多关系 引发的错误处理
  4. AIX 系统中 PVID 的含义与作用
  5. iOS 编译报错
  6. 重读gets()与is函数的用法
  7. ubuntu NFS
  8. 华为s5700 添加与删除vlan
  9. JavaEE XML DOM创建
  10. CentOS下编译安装Apache2(新)
  11. mongo安装,及远程连接
  12. HTML基础教程-段落
  13. 前端学习_02_vps、web服务器、域名申请
  14. 【数据库】数据库的锁机制,MySQL中的行级锁,表级锁,页级锁
  15. Redis学习笔记~Twenproxy所起到的作用
  16. Windows系统资源监控
  17. bzoj-1179(缩点+最短路)
  18. 第十六次 ccf 201903-2 二十四点
  19. python基础 (函数名,闭包,和迭代器)
  20. Django模板渲染

热门文章

  1. ES6知识总结
  2. MongoDB运维心得(一)
  3. Python数据分析Numpy库方法简介(一)
  4. 大话npm,cnpm和yarn
  5. vue组件传值
  6. 彻底理解Netty,这一篇文章就够了
  7. 怎样从外网访问内网OpenLDAP数据库
  8. SQL Server 2012安装时报错,错误 0x80070422怎么解决?解决方法。
  9. Flutter 获取控件尺寸和位置
  10. mysql 热备份