获取路径下所有文件

#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <string>
#include <vector>
#include <io.h>
using namespace std;

void GetAllFiles(string path, vector<string>& files)
{
	long hfile = 0;
	struct _finddata_t fileinfo;
	string p;
	if ((hfile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) == NO_ERROR);
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != NO_ERROR)
					GetAllFiles(p.assign(path).append("/").append(fileinfo.name), files);
			}
			else
			{
				files.push_back(p.assign(path).append("/").append(fileinfo.name));
			}
		} while (_findnext(hfile, &fileinfo) == NO_ERROR);
		_findclose(hfile);
	}
}

int main()
{
	vector<string> files;
	string filebuf;
	char modulbuf[FILENAME_MAX];
	GetModuleFileName(NULL, modulbuf, FILENAME_MAX);

	filebuf.append(modulbuf);
	string path(filebuf.substr(0, filebuf.find_last_of("\\")));

	GetAllFiles(path, files);

	for (vector<string>::iterator it = files.begin(); it != files.end(); it++){
		cout << it->c_str() << endl;
	}
	return 0;
}


Example:


另外

/* File attribute constants for _findfirst() */

#define _A_NORMAL       0x00    /* Normal file - No read/write restrictions */
#define _A_RDONLY       0x01    /* Read only file */
#define _A_HIDDEN       0x02    /* Hidden file */
#define _A_SYSTEM       0x04    /* System file */
#define _A_SUBDIR       0x10    /* Subdirectory */
#define _A_ARCH         0x20    /* Archive file */

_A_HIDDEN
可以用于查看隐藏属性文件。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值