_findfirst和_findnext

1、首先是_finddata结构体,用于存储文件信息的结构体。

2、_findfirst函数:long _findfirst(const char *, struct _finddata_t *);

    第一个参数为文件名,第二个参数为_finddata结构体指针,若成功,则返回文件的句柄。

3、_findnext函数:int _findnext(long, struct _finddata_t *);

    第一个参数为文件句柄,表示读下一个。

4、_findclose()函数:int _findclose(long);

    将该文件句柄关闭

以下为一个小例子

 

void getFiles(string path, vector<string>& files)
{
    long hFile = 0;
    struct _finddata_t fileinfo;//存储文件信息的结构体
    string p;
    //c_str将string转为C下的字符,hFile得到的是文件句柄
    if ((hFile = _findfirst(p.assign(path).append("\\*.jpg").c_str(), &fileinfo)) != -1)
    {
        do
        {
            files.push_back(p.assign(path).append("\\").append(fileinfo.name));

        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

 参考:http://blog.sina.com.cn/s/blog_53988e5a0101dvlf.html

         http://blog.csdn.net/samylee/article/details/53467668

 

posted @ 2017-04-18 08:44  牧马人夏峥  阅读(2244)  评论(0编辑  收藏  举报