遍历读取指定文件夹下指定类型的所有文件

这篇博客提供了一段C++代码示例,用于遍历指定路径下的文件夹,并查找具有特定扩展名的文件。程序首先定义了一个用于存储文件名的vector,然后使用FindFirstFile和FindNextFile函数来搜索文件。找到的文件通过dwFileAttributes判断是目录还是文件,将文件路径添加到vector中,最后进行排序并打印出来。
摘要由CSDN通过智能技术生成

经常碰到朋友,尤其是初学者对指定文件夹下指定类型文件的读取很困惑,这里,我把自己经常用的程序贴出来,供初学者学些;

 

#include "stdafx.h"
#include "windows.h"
#include <vector>
#include <string>
#include "iostream"
using namespace std;
typedef std::vector<std::string> file_lists;

 

static int str_compare(const void *arg1, const void *arg2)
{
       return strcmp((*(std::string*)arg1).c_str(), (*(std::string*)arg2).c_str());//比较字符串arg1 and arg2
}

 

file_lists ScanDirectory(const std::string &path, const std::string &extension)
{
    WIN32_FIND_DATA wfd;//WIN32_FIND_DATA:Contains information about the file that is found by the
        //FindFirstFile, FindFirstFileEx, or FindNextFile function
     HANDLE hHandle;
     string searchPath, searchFile;
     file_lists vFilenames;
 

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值