VC中有关目录操作的函数

目录操作函数:

#include <io.h>
#include <errno.h>

原型:int access(char* pathname, int mode);
参数:mode
-- 00(仅检查是否存在)
-- 02(检查写权限)
-- 04(检查读权限)
-- 06(检查读写权限)

返回值:如果文件具有指定模式则返回0,如果返回-1则表示指定文件不存在或不能按指定模式进行存取。


///

如果是在Windows下,则可以使用GetFileAttributes() API来获取文件夹的属性:

DWORD GetFileAttributes(
  LPCTSTR lpFileName   // name of file or directory
);

返回值:

FILE_ATTRIBUTE_ARCHIVE
[The file or directory is an archive file or directory. Applications use this attribute to mark files for backup or removal. ]

FILE_ATTRIBUTE_COMPRESSED
[The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.] 

FILE_ATTRIBUTE_DEVICE
[Reserved; do not use.]
 
FILE_ATTRIBUTE_DIRECTORY
[The handle identifies a directory.]
 
FILE_ATTRIBUTE_ENCRYPTED
[The file or directory is encrypted. For a file, this means that all data streams in the file are encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories.]
 
FILE_ATTRIBUTE_HIDDEN
[The file or directory is hidden. It is not included in an ordinary directory listing. ]

FILE_ATTRIBUTE_NORMAL
[The file or directory has no other attributes set. This attribute is valid only if used alone. ]

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
[ The file will not be indexed by the content indexing service. ]

FILE_ATTRIBUTE_OFFLINE
[The data of the file is not immediately available. This attribute indicates that the file data has been physically moved to offline storage. This attribute is used by Remote Storage, the hierarchical storage management software. Applications should not arbitrarily change this attribute. ]

FILE_ATTRIBUTE_READONLY
[The file or directory is read-only. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it. ]

FILE_ATTRIBUTE_REPARSE_POINT
[The file has an associated reparse point. ]

FILE_ATTRIBUTE_SPARSE_FILE
[ The file is a sparse file. ]

FILE_ATTRIBUTE_SYSTEM
[ The file or directory is part of, or is used exclusively by, the operating system. ]

FILE_ATTRIBUTE_TEMPORARY
[The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed. ]

例如:检查文件夹是否只读

BOOL DirectoryIsReadonly(char* szPath)
{
  DWORD dwAttr = GetFileAttributes(szPath);

  //是文件夹 && 只读
  return ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0) && ((dwAttr & FILE_ATTRIBUTE_READONLY) != 0);
}

检查其它属性类推。


其他目录操作函数:

1 . int _access( const char *path, int mode );
功   能 : 测定文件/目录存取权限.
头文件 : #include <io.h>
参   数 : path:文件或者目录
   mode:权限设定,其值如下:
   00 Existence only
   02 Write permission
   04 Read permission
   06 Read and write permission

返回值 : 拥有该权限返回0
   没有权限返回-1,且设置errno为如下值
   ENOENT 路径/文件不存在
   EACCES 没有相应权限

2 . int _chdir( const char *dirname );
功   能 : 更改当前工作目录.
头文件 : #include <direct.h>
返回值 : 成功返回0
   失败返回-1,且设置errno如下:
   ENOENT 该路径不存在

3 . int _chdrive( int drive );
功   能 : 更改当前工作驱动器.
头文件 : #include <direct.h>
返回值 : 成功返回0
   失败返回-1
注   释 : 参数说明
     drive =1<==> A盘
     drive =2<==> B盘
     drive =3<==> C盘
   如此等等,该函数可以由_chdir代替

4 . int _findclose( long handle );
功   能 : 关闭搜寻句柄并释放相应资源
头文件 : #include <io.h>
参   数 : long handle 搜索句柄(通常由紧靠其前的_findfirst()返回,_findfirst()见下)
   fileinfo 文件信息buffer
返回值 : 成功返回0
   出错返回-1,且设置errno为如下值
   ENOENT 没有更多的符合该泛式的文件

5 . long _findfirst( char *filespec, struct _finddata_t *fileinfo );
功   能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext函数后续使用来完成某泛式下的文件遍历.
头文件 : #include <io.h>
参   数 : filespec 目标文件规范,可以包含通配符
   fileinfo 文件信息buffer
返回值 : 成功返回唯一的搜索句柄
   出错返回-1,且设置errno为如下值
   ENOENT 该泛式无法匹配
   EINVAL 无效文件名

6 . int _findnext( long handle, struct _finddata_t *fileinfo );
功   能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以 此为依据修改fileinfo中的值
头文件 : #include <io.h>
参   数 : long handle 搜索句柄(通常由紧靠其前的_findfirst()返回)
   fileinfo 文件信息buffer
返回值 : 成功返回0
   出错返回-1,且设置errno为如下值
   ENOENT 没有更多的符合该泛式的文件


7 . char *_getcwd( char *buffer, int maxlen );
功   能 : 获得当前工作目录.
头文件 : #include <direct.h>
返回值 : 成功返回指向buffer的pointer
   失败返回NULL,且设置errno为以下三个值之一:
   ENODEV 无该设备
   ENOMEM 内存不够
   ERANGE 结果超出范围
注   意 : 当第一个参数为 NULL 时,第二个参数 maxlen 长度设置无效,且函数使用malloc分配足够内存,需要将函数返回值传递给free()函数来释放内存.
   当第一个参数不为 NULL 时,maxlen 指定长度不够函数返回错,设置errno为ERANGE

8 . char *_getdcwd( int drive, char *buffer, int maxlen );
功   能 : 获得指定驱动器的当前工作路径.
头文件 : #include <direct.h>
返回值 : 成功返回指向buffer的pointer
   失败返回NULL,且设置errno为以下三个值之一:
   ENODEV 无该设备
   ENOMEM 内存不够
   ERANGE 结果超出范围
注   意 : 当第一个参数为 NULL 时,该函数设置errno为ERANGE

9 . int _getdrive( void );
功   能 : 获得当前磁盘驱动器.
头文件 : #include <direct.h>
返回值 : 返回驱动器值,1<==>A 2<==>B 如此等等;函数不会出错!

10 . unsigned long _getdrives(void);
功   能 : 获得当前所有驱动器.
头文件 : #include <direct.h>
返回值 : 各个位代表对应驱动器,
   bit 0 <==> A
   bit 1 <==> B
   bit 2 <==> C
   ... ...
注:bit x 表示unsigned long的第x位

11 . int _mkdir( const char *dirname );
功   能 : 创建一个新目录,目录名为dirname.
头文件 : #include <direct.h>
返回值 : 成功返回0
   失败返回-1,且设置errno为以下三个值之一
   EACCESS 权限不允许
   EEXIST   该目录已存在
   ENOENT   无该文件或目录

12 . int _rmdir( const char *dirname );
功   能 : 删除名为dirname的目录.
头文件 : #include <direct.h>
返回值 : 成功返回0
   失败返回-1,且设置errno为以下三个值之一
   EACCESS    权限不允许
   ENOTEMPTY dirname不是文件夹;
      或者该文件夹不空;
      或者dirname为当前工作文件夹;
      或者dirname为当根文件夹;
   ENOENT     无该文件或目录

//--end--




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值