您的位置:首页 > 编程语言 > C语言/C++

C语言windows目录操作

2014-11-17 10:35 405 查看
http://blog.csdn.net/yangalbert/article/details/7455241

此外还有FindFirst FindNext等,但是这些会涉及到字符集的转换,后期再研究

1.获得当前工作目录

[cpp]
view plaincopy

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 plaincopy

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

3. 文件遍历(查找)

[cpp]
view plaincopy

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 plaincopy

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

5. 删除目录

[cpp]
view plaincopy

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

6. 其他操作

[cpp]
view plaincopy

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 plaincopy

// 功  能 : 打印目录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 );  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: