c++遍历目录用到的几个函数

本文介绍了C++中用于遍历目录的几个关键函数,包括access()、_chdir()以及_findclose()。access函数用于检查文件或目录的访问权限,_chdir则用于改变当前工作目录,而_findclose则是在完成文件查找后用于关闭查找句柄的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );

功 能: 确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。如果指定的存取方式有效,则函数返回0,否则函数返回-1。
#include <io.h>
参数说明:
filenpath
文件或文件夹的路径, 当前目录直接使用文件或文件夹名
备注:当该参数为文件的时候,access函数能使用mode参数所有的值,当该参数为文件夹的时候,access函数值能判断文件夹是否存在。在WIN NT 中,所有的文件夹都有读和写权限
mode
要判断的模式
在头文件unistd.h中的预定义如下:
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
具体含义如下:
R_OK 只判断是否有读权限
W_OK 只判断是否有写权限
X_OK 判断是否有执行权限
F_OK 只判断是否存在

 

int _chdir(const char *path);

头文件:direct.h

功 能: 改变当前工作目录

参 数:Path 必选。Path 可能包含驱动器。如果未指定驱动器,则当前驱动器上的默认目录或文件夹。

返回值:成功返回0 ,失败返回-1
异 常 :异常类型 错误号 条件
ArgumentException   52   Path 为空。
FileNotFoundException   76   指定的驱动器无效,或驱动器不可用。
 
备 注: ChDir 函数更改默认目录,但是不更改默认驱动器。例如,如果默认驱动器是 C,下面的语句更改驱动器 D 上的默认目录,但 C 仍为默认驱动器:
安全注意:  ChDir 函数需要 非托管代码权限,这可能会对它在部分信任情况下的执行产生影响。有关更多信息,请参见 SecurityPermission 类和代码 访问权限

 

函数原型:long _findfirst( char *filespec, struct _finddata_t *fileinfo );
函数功能:搜索与指定的文件名称匹配的第一个实例,若成功则返回第一个实例的句柄,否则返回-1L
头文件:io.h

 

 

函数原型:
int _findnext(intptr_t handle,struct _finddata_t *fileinfo );
所属库:io.h
函数功能:搜索与_ findfirst函数提供的文件名称匹配的下一个实例,若成功则返回0,否则返回-1
参数说明:
struct _finddata_t的定义见于io.h
struct _finddata_t {
unsigned    attrib;
time_t      time_create;    /* -1 for FAT file systems */
time_t      time_access;    /* -1 for FAT file systems */
time_t      time_write;
_fsize_t    size;
char        name[260];
};

 

int _findclose(    intptr_t handle );

/*关闭查找*/

Parameters

handle Search handle returned by a previous call to _findfirst. Return Value

If successful, _findclose returns 0. Otherwise, it returns –1 and setserrno to ENOENT, indicating that no more matching files could be found.

_findclose

<io.h>

 

 

#include "io.h"
#include "direct.h"

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;


#include <string>
using std::string;


int main()
{
	string dir;
	cout << "Input the name of directory: ";
	cin >> dir;

	if (_access(dir.c_str(), 06) == -1)
	{
		cerr << "error: directory does not exist." << endl;
		exit(-1);
	}

	if (dir.at(dir.length() - 1) != '\\')
	{
		dir += '\\';
	}

	if (_chdir(dir.c_str()) != 0)
	{
		cerr << "error: function _chdir() failed.";
		exit(-1);
	}

	_finddata_t fileinfo;
	memset(&fileinfo, 0x0, sizeof(fileinfo));	

	
	intptr_t iFind = _findfirst("*", &fileinfo);
	if (iFind == -1)
	{
		cerr << "error: function _findfirst failed." << endl;
		exit(-1);
	}


	string filePath(dir + fileinfo.name);

	cout << "name: " << filePath << endl;

	
	while (_findnext(iFind, &fileinfo) == 0)
	{
		filePath = dir + fileinfo.name;
		cout << "name: " << filePath << endl;
	}

	_findclose(iFind);


	return 0;
}

上面代码转自:http://blog.youkuaiyun.com/selina6874/article/details/6784697
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

转瞬之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值