C语言windows目录操作
2015-04-14 11:58
316 查看
1.获得当前工作目录
[cpp]
view plaincopyprint?
char* _getcwd( char *buffer, int maxlen );
// 功 能 : 获得当前工作目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回指向buffer的pointer
// 失败返回NULL,且设置errno为以下三个值之一:
// ENODEV 无该设备
// ENOMEM 内存不够
// ERANGE 结果超出范围
// 注 意 : 当第一个参数为 NULL 时, 第二个参数 maxlen 长度设置无效,且函数
// 使用 malloc 分配足够内存, 需要将函数返回值传递给 free() 函数来
// 释放内存. 当第一个参数不为 NULL 时,maxlen 指定长度不够函数返回
// 错,设置errno为ERANGE
2. 更改当前工作目录
[cpp]
view plaincopyprint?
int _chdir( const char *dirname );
// 功 能 : 更改当前工作目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno如下:
// ENOENT 该路径不存在
3. 文件遍历(查找)
[cpp]
view plaincopyprint?
long _findfirst( char *filespec, struct _finddata_t *fileinfo );
// 功 能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext函
// 数后续使用来完成某泛式下的文件遍历.
// 头文件 : #include <io.h>
// 参 数 : filespec - 目标文件规范,可以包含通配符
// fileinfo - 文件信息buffer
// 返回值 : 成功返回唯一的搜索句柄
// 出错返回-1,且设置errno为如下值:
// ENOENT 该泛式无法匹配
// EINVAL 无效文件名
// 注 意 : _finddata_t 说明
struct _finddata_t
{
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[_MAX_FNAME];
};
// 其中 :
// unsigned atrrib : 文件属性的存储位置。它存储一个unsigned单元,用于表示文件的
// 属性。文件属性是用位表示的,主要有以下一些:_A_ARCH(存档)、
// _A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、
// _A_SUBDIR(文件夹)、_A_SYSTEM(系统)。这些都是在<io.h>中
// 定义的宏,可以直接使用,而本身的意义其实是一个无符号整型
// (只不过这个整型应该是2的几次幂,从而保证只有一位为1,而其他
// 位为0)。既然是位表示,那么当一个文件有多个属性时,它往往是
// 通过位或的方式,来得到几个属性的综合。例如只读+隐藏+系统属性,
// 应该为:_A_HIDDEN | _A_RDONLY |_A_SYSTEM 。
// time_t time_create:这里的time_t是一个变量类型,用来存储文件创建时间。
// time_t time_access: 文件最后一次被访问的时间。
// time_t time_write : 文件最后一次被修改的时间。
// _fsize_t size : 文件的大小。这里的_fsize_t应该可以相当于unsigned整型,表示
// 文件的字节数。
// char name[_MAX_FNAME]:文件的文件名。这里的_MAX_FNAME是一个常量宏,它在<stdlib.h>头
// 文件中被定义,表示的是文件名的最大长度。
int _findnext( long handle, struct _finddata_t *fileinfo );
// 功 能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以此为依据
// 修改fileinfo中的值
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// fileinfo - 文件信息buffer
// 返回值 : 成功返回0
// 出错返回-1,且设置errno为如下值:
// ENOENT 没有更多的符合该泛式的文件
int _findclose( long handle );
// 功 能 : 关闭搜寻句柄并释放相应资源
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// 返回值 : 成功返回0
// 出错返回-1,且设置errno为如下值:
// ENOENT 没有更多的符合该泛式的文件
4. 创建目录
[cpp]
view plaincopyprint?
int _mkdir( const char *dirname );
// 功 能 : 创建一个新目录,目录名为dirname.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno为以下三个值之一:
// EACCESS 权限不允许
// EEXIST 该目录已存在
// ENOENT 无该文件或目录
5. 删除目录
[cpp]
view plaincopyprint?
int _rmdir( const char *dirname );
// 功 能 : 删除名为dirname的目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno为以下三个值之一:
// EACCESS : 权限不允许
// ENOTEMPTY : dirname不是文件夹;或者该文件夹不空;或
// 者dirname为当前工作文件夹;或者dirname
// 为当根文件夹;
// ENOENT : 无该文件或目录
6. 其他操作
[cpp]
view plaincopyprint?
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
int _chdrive( int drive );
// 功 能 : 更改当前工作驱动器.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1
// 注 释 : 参数说明
// drive =1 : A盘
// drive =2 : B盘
// drive =3 : C盘 ...
char* _getdcwd( int drive, char *buffer, int maxlen );
// 功 能 : 获得指定驱动器的当前工作路径.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回指向buffer的pointer
// 失败返回NULL,且设置errno为以下三个值之一:
// ENODEV 无该设备
// ENOMEM 内存不够
// ERANGE 结果超出范围
// 注 意 : 当第一个参数为 NULL 时,该函数设置errno为ERANGE
测试:
[cpp]
view plaincopyprint?
// 功 能 : 打印目录path中与模式chRE匹配的所有文件明 // 输 入 : path - 待打印的目录 // chRE - 要求匹配的正则表达式 static void printDir( const char* path, const char* chRE ) { char* chCurPath = getcwd( NULL, 0); // 当前工作目录 printf("current work path: %s\n", chCurPath ); int ret = _chdir( path ); if ( ret < 0 ) { perror( path ); } char* newPath = getcwd( NULL, 0 ); printf("new work path: %s\n", newPath); free(newPath); struct _finddata_t data; long hnd = _findfirst( chRE, &data ); // 查找文件名与正则表达式chRE的匹配第一个文件 // 返回唯一的搜索句柄 if ( hnd < 0 ) { perror( chRE ); } int nRet = (hnd <0 ) ? -1 : 1; while ( nRet >= 0 ) { if ( data.attrib == _A_SUBDIR ) // 如果是目录 printf(" [%s]*\n", data.name ); else printf(" [%s]\n", data.name ); nRet = _findnext( hnd, &data ); } _findclose( hnd ); // 关闭当前句柄 chdir( chCurPath); // 切换回之前的工作目录 free( chCurPath ); }
参考: http://hi.baidu.com/cppgp/blog/item/1013c550df50ef5d1138c274.html
http://wenku.baidu.com/view/da9855b9960590c69ec376bf.html
[cpp]
view plaincopyprint?
char* _getcwd( char *buffer, int maxlen );
// 功 能 : 获得当前工作目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回指向buffer的pointer
// 失败返回NULL,且设置errno为以下三个值之一:
// ENODEV 无该设备
// ENOMEM 内存不够
// ERANGE 结果超出范围
// 注 意 : 当第一个参数为 NULL 时, 第二个参数 maxlen 长度设置无效,且函数
// 使用 malloc 分配足够内存, 需要将函数返回值传递给 free() 函数来
// 释放内存. 当第一个参数不为 NULL 时,maxlen 指定长度不够函数返回
// 错,设置errno为ERANGE
char* _getcwd( char *buffer, int maxlen ); // 功 能 : 获得当前工作目录. // 头文件 : #include <direct.h> // 返回值 : 成功返回指向buffer的pointer // 失败返回NULL,且设置errno为以下三个值之一: // ENODEV 无该设备 // ENOMEM 内存不够 // ERANGE 结果超出范围 // 注 意 : 当第一个参数为 NULL 时, 第二个参数 maxlen 长度设置无效,且函数 // 使用 malloc 分配足够内存, 需要将函数返回值传递给 free() 函数来 // 释放内存. 当第一个参数不为 NULL 时,maxlen 指定长度不够函数返回 // 错,设置errno为ERANGE
2. 更改当前工作目录
[cpp]
view plaincopyprint?
int _chdir( const char *dirname );
// 功 能 : 更改当前工作目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno如下:
// ENOENT 该路径不存在
int _chdir( const char *dirname ); // 功 能 : 更改当前工作目录. // 头文件 : #include <direct.h> // 返回值 : 成功返回0 // 失败返回-1,且设置errno如下: // ENOENT 该路径不存在
3. 文件遍历(查找)
[cpp]
view plaincopyprint?
long _findfirst( char *filespec, struct _finddata_t *fileinfo );
// 功 能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext函
// 数后续使用来完成某泛式下的文件遍历.
// 头文件 : #include <io.h>
// 参 数 : filespec - 目标文件规范,可以包含通配符
// fileinfo - 文件信息buffer
// 返回值 : 成功返回唯一的搜索句柄
// 出错返回-1,且设置errno为如下值:
// ENOENT 该泛式无法匹配
// EINVAL 无效文件名
// 注 意 : _finddata_t 说明
struct _finddata_t
{
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[_MAX_FNAME];
};
// 其中 :
// unsigned atrrib : 文件属性的存储位置。它存储一个unsigned单元,用于表示文件的
// 属性。文件属性是用位表示的,主要有以下一些:_A_ARCH(存档)、
// _A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、
// _A_SUBDIR(文件夹)、_A_SYSTEM(系统)。这些都是在<io.h>中
// 定义的宏,可以直接使用,而本身的意义其实是一个无符号整型
// (只不过这个整型应该是2的几次幂,从而保证只有一位为1,而其他
// 位为0)。既然是位表示,那么当一个文件有多个属性时,它往往是
// 通过位或的方式,来得到几个属性的综合。例如只读+隐藏+系统属性,
// 应该为:_A_HIDDEN | _A_RDONLY |_A_SYSTEM 。
// time_t time_create:这里的time_t是一个变量类型,用来存储文件创建时间。
// time_t time_access: 文件最后一次被访问的时间。
// time_t time_write : 文件最后一次被修改的时间。
// _fsize_t size : 文件的大小。这里的_fsize_t应该可以相当于unsigned整型,表示
// 文件的字节数。
// char name[_MAX_FNAME]:文件的文件名。这里的_MAX_FNAME是一个常量宏,它在<stdlib.h>头
// 文件中被定义,表示的是文件名的最大长度。
int _findnext( long handle, struct _finddata_t *fileinfo );
// 功 能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以此为依据
// 修改fileinfo中的值
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// fileinfo - 文件信息buffer
// 返回值 : 成功返回0
// 出错返回-1,且设置errno为如下值:
// ENOENT 没有更多的符合该泛式的文件
int _findclose( long handle );
// 功 能 : 关闭搜寻句柄并释放相应资源
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// 返回值 : 成功返回0
// 出错返回-1,且设置errno为如下值:
// ENOENT 没有更多的符合该泛式的文件
long _findfirst( char *filespec, struct _finddata_t *fileinfo ); // 功 能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext函 // 数后续使用来完成某泛式下的文件遍历. // 头文件 : #include <io.h> // 参 数 : filespec - 目标文件规范,可以包含通配符 // fileinfo - 文件信息buffer // 返回值 : 成功返回唯一的搜索句柄 // 出错返回-1,且设置errno为如下值: // ENOENT 该泛式无法匹配 // EINVAL 无效文件名 // 注 意 : _finddata_t 说明 struct _finddata_t { unsigned attrib; time_t time_create; time_t time_access; time_t time_write; _fsize_t size; char name[_MAX_FNAME]; }; // 其中 : // unsigned atrrib : 文件属性的存储位置。它存储一个unsigned单元,用于表示文件的 // 属性。文件属性是用位表示的,主要有以下一些:_A_ARCH(存档)、 // _A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、 // _A_SUBDIR(文件夹)、_A_SYSTEM(系统)。这些都是在<io.h>中 // 定义的宏,可以直接使用,而本身的意义其实是一个无符号整型 // (只不过这个整型应该是2的几次幂,从而保证只有一位为1,而其他 // 位为0)。既然是位表示,那么当一个文件有多个属性时,它往往是 // 通过位或的方式,来得到几个属性的综合。例如只读+隐藏+系统属性, // 应该为:_A_HIDDEN | _A_RDONLY |_A_SYSTEM 。 // time_t time_create:这里的time_t是一个变量类型,用来存储文件创建时间。 // time_t time_access: 文件最后一次被访问的时间。 // time_t time_write : 文件最后一次被修改的时间。 // _fsize_t size : 文件的大小。这里的_fsize_t应该可以相当于unsigned整型,表示 // 文件的字节数。 // char name[_MAX_FNAME]:文件的文件名。这里的_MAX_FNAME是一个常量宏,它在<stdlib.h>头 // 文件中被定义,表示的是文件名的最大长度。 int _findnext( long handle, struct _finddata_t *fileinfo ); // 功 能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以此为依据 // 修改fileinfo中的值 // 头文件 : #include <io.h> // 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回) // fileinfo - 文件信息buffer // 返回值 : 成功返回0 // 出错返回-1,且设置errno为如下值: // ENOENT 没有更多的符合该泛式的文件 int _findclose( long handle ); // 功 能 : 关闭搜寻句柄并释放相应资源 // 头文件 : #include <io.h> // 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回) // 返回值 : 成功返回0 // 出错返回-1,且设置errno为如下值: // ENOENT 没有更多的符合该泛式的文件
4. 创建目录
[cpp]
view plaincopyprint?
int _mkdir( const char *dirname );
// 功 能 : 创建一个新目录,目录名为dirname.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno为以下三个值之一:
// EACCESS 权限不允许
// EEXIST 该目录已存在
// ENOENT 无该文件或目录
int _mkdir( const char *dirname ); // 功 能 : 创建一个新目录,目录名为dirname. // 头文件 : #include <direct.h> // 返回值 : 成功返回0 // 失败返回-1,且设置errno为以下三个值之一: // EACCESS 权限不允许 // EEXIST 该目录已存在 // ENOENT 无该文件或目录
5. 删除目录
[cpp]
view plaincopyprint?
int _rmdir( const char *dirname );
// 功 能 : 删除名为dirname的目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1,且设置errno为以下三个值之一:
// EACCESS : 权限不允许
// ENOTEMPTY : dirname不是文件夹;或者该文件夹不空;或
// 者dirname为当前工作文件夹;或者dirname
// 为当根文件夹;
// ENOENT : 无该文件或目录
int _rmdir( const char *dirname ); // 功 能 : 删除名为dirname的目录. // 头文件 : #include <direct.h> // 返回值 : 成功返回0 // 失败返回-1,且设置errno为以下三个值之一: // EACCESS : 权限不允许 // ENOTEMPTY : dirname不是文件夹;或者该文件夹不空;或 // 者dirname为当前工作文件夹;或者dirname // 为当根文件夹; // ENOENT : 无该文件或目录
6. 其他操作
[cpp]
view plaincopyprint?
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
int _chdrive( int drive );
// 功 能 : 更改当前工作驱动器.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
// 失败返回-1
// 注 释 : 参数说明
// drive =1 : A盘
// drive =2 : B盘
// drive =3 : C盘 ...
char* _getdcwd( int drive, char *buffer, int maxlen );
// 功 能 : 获得指定驱动器的当前工作路径.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回指向buffer的pointer
// 失败返回NULL,且设置errno为以下三个值之一:
// ENODEV 无该设备
// ENOMEM 内存不够
// ERANGE 结果超出范围
// 注 意 : 当第一个参数为 NULL 时,该函数设置errno为ERANGE
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 int _chdrive( int drive ); // 功 能 : 更改当前工作驱动器. // 头文件 : #include <direct.h> // 返回值 : 成功返回0 // 失败返回-1 // 注 释 : 参数说明 // drive =1 : A盘 // drive =2 : B盘 // drive =3 : C盘 ... char* _getdcwd( int drive, char *buffer, int maxlen ); // 功 能 : 获得指定驱动器的当前工作路径. // 头文件 : #include <direct.h> // 返回值 : 成功返回指向buffer的pointer // 失败返回NULL,且设置errno为以下三个值之一: // ENODEV 无该设备 // ENOMEM 内存不够 // ERANGE 结果超出范围 // 注 意 : 当第一个参数为 NULL 时,该函数设置errno为ERANGE
测试:
[cpp]
view plaincopyprint?
// 功 能 : 打印目录path中与模式chRE匹配的所有文件明 // 输 入 : path - 待打印的目录 // chRE - 要求匹配的正则表达式 static void printDir( const char* path, const char* chRE ) { char* chCurPath = getcwd( NULL, 0); // 当前工作目录 printf("current work path: %s\n", chCurPath ); int ret = _chdir( path ); if ( ret < 0 ) { perror( path ); } char* newPath = getcwd( NULL, 0 ); printf("new work path: %s\n", newPath); free(newPath); struct _finddata_t data; long hnd = _findfirst( chRE, &data ); // 查找文件名与正则表达式chRE的匹配第一个文件 // 返回唯一的搜索句柄 if ( hnd < 0 ) { perror( chRE ); } int nRet = (hnd <0 ) ? -1 : 1; while ( nRet >= 0 ) { if ( data.attrib == _A_SUBDIR ) // 如果是目录 printf(" [%s]*\n", data.name ); else printf(" [%s]\n", data.name ); nRet = _findnext( hnd, &data ); } _findclose( hnd ); // 关闭当前句柄 chdir( chCurPath); // 切换回之前的工作目录 free( chCurPath ); }
// 功 能 : 打印目录path中与模式chRE匹配的所有文件明 // 输 入 : path - 待打印的目录 // chRE - 要求匹配的正则表达式 static void printDir( const char* path, const char* chRE ) { char* chCurPath = getcwd( NULL, 0); // 当前工作目录 printf("current work path: %s\n", chCurPath ); int ret = _chdir( path ); if ( ret < 0 ) { perror( path ); } char* newPath = getcwd( NULL, 0 ); printf("new work path: %s\n", newPath); free(newPath); struct _finddata_t data; long hnd = _findfirst( chRE, &data ); // 查找文件名与正则表达式chRE的匹配第一个文件 // 返回唯一的搜索句柄 if ( hnd < 0 ) { perror( chRE ); } int nRet = (hnd <0 ) ? -1 : 1; while ( nRet >= 0 ) { if ( data.attrib == _A_SUBDIR ) // 如果是目录 printf(" [%s]*\n", data.name ); else printf(" [%s]\n", data.name ); nRet = _findnext( hnd, &data ); } _findclose( hnd ); // 关闭当前句柄 chdir( chCurPath); // 切换回之前的工作目录 free( chCurPath ); }
参考: http://hi.baidu.com/cppgp/blog/item/1013c550df50ef5d1138c274.html
http://wenku.baidu.com/view/da9855b9960590c69ec376bf.html
相关文章推荐
- C语言windows目录操作
- C语言遍历windows目录文件 并对文件进行读写操作
- C语言windows目录操作
- C语言windows目录操作
- C语言windows目录操作
- [C语言]系统目录操作函数
- C语言中改变目录的相关操作函数详解
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
- python 操作windows下的目录[转]
- c语言目录操作总结
- windows下对目录进行的操作函数总结
- C语言中关于对目录的操作
- 【修改彻底,操作方便】Windows 7改变User目录到别的盘的方法
- 支持Windows和Linux的文件目录创建和删除操作
- C语言编程中对目录进行基本的打开关闭和读取操作详解
- Windows程序设计零基础自学_14_Windows文件和目录操作
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
- python 操作windows下的目录 分类: python python基础学习 2013-07-30 17:30 274人阅读 评论(0) 收藏
- 【修改彻底,操作方便】Windows 7改变User目录到别的盘的方法
- 在Linux下和Windows下遍历目录的方法及如何达成一致性操作