• c语言目录操作总结


    ===================================================
    char *getcwd( char *buffer, int maxlen ); (获取当前目录)
    // 功  能 : 获得当前工作目录.  
    // 头文件 : #include <direct.h>  
    // 返回值 : 成功返回指向buffer的pointer  
    //          失败返回NULL,且设置errno为以下三个值之一:  
    //            ENODEV 无该设备  
    //            ENOMEM 内存不够  
    //            ERANGE 结果超出范围  
    // 注  意 : 当第一个参数为 NULL 时, 第二个参数 maxlen 长度设置无效,且函数  
    //          使用 malloc 分配足够内存, 需要将函数返回值传递给 free() 函数来  
    //          释放内存. 当第一个参数不为 NULL 时,maxlen 指定长度不够函数返回  
    //          错误,设置errno为ERANGE  
    ====================================================
    int chdir( const char *dirname );  (改变当前目录)
    // 功  能 : 更改当前工作目录.  
    // 头文件 : #include <direct.h>  
    // 返回值 : 成功返回0  
    //          失败返回-1,且设置errno如下:  
    //            ENOENT 该路径不存在  
    ====================================================
    long _findfirst( char *filespec, struct _finddata_t *fileinfo );   
    // 功  能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext函  
    //          数后续使用来完成某泛式下的文件遍历.  
    // 头文件 : #include <io.h>  
    // 参  数 : filespec - 指定的目录名或文件名,如"D:\*.txt" 或 “*.*”    
    //          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 没有更多的符合该泛式的文件  
    
    ====================================================
    int mkdir( const char *dirname );  
    // 功  能 : 创建一个新目录,目录名为dirname.  
    // 头文件 : #include <direct.h>  
    // 返回值 : 成功返回0  
    //          失败返回-1,且设置errno为以下三个值之一:  
    //            EACCESS 权限不允许  
    //            EEXIST   该目录已存在  
    //            ENOENT   无该文件或目录 
    
    ====================================================
    int rmdir( const char *dirname );  
    // 功  能 : 删除名为dirname的目录.  
    // 头文件 : #include <direct.h>  
    // 返回值 : 成功返回0  
    //          失败返回-1,且设置errno为以下三个值之一:  
    //            EACCESS   : 权限不允许  
    //            ENOTEMPTY : dirname不是文件夹;或者该文件夹不空;或  
    //                        者dirname为当前工作文件夹;或者dirname  
    //                        为当根文件夹;  
    //            ENOENT    : 无该文件或目录
    
    ====================================================
    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 getdisk() 取当前正在使用的驱动器,返回一个整数(0=A,1=B,2=C等)  
    
    ====================================================
    char *mktemp(char *template) 构造一个当前目录上没有的文件名并存于template中
    
    ====================================================
    int fnsplit(char *path,char *drive,char *dir,char *name,char *ext)  
    此函数将文件名path分解成盘符drive(C:、A:等),路径dir(TC、BCLIB等),文件名name(TC、WPS等),
    扩展名ext(.EXE、.COM等),并分别存入相应的变量中.  
    
    ====================================================
    void fumerge(char *path,char *drive,char *dir,char *name,char *ext)  
    此函数通过盘符drive(C:、A:等),路径dir(TC、BCLIB等),文件名name(TC、WPS等),扩展名ext(.EXE、.COM等)
    组成一个文件名,存在path中.  
    
    ====================================================
    范例:
    #include <stdio.h>
    #include <string.h>
    #include <direct.h>
    #include <malloc.h>
    #include <io.h>
    
        /*
                int SearchPath(char *pszPath)
                {
                    char szBuf[100];
                    memset(szBuf, 0, sizeof(szBuf));
    
                    if (NULL != getcwd(szBuf, sizeof(szBuf)))
                    
                        printf("%s
    ", szBuf);
                    }
    
                    int rv = 0;
                    rv = chdir("C:\Users\SKS\Desktop\0114文件操作");
                    if (0 != rv)
                    {
                        printf("func chdir() error
    ");
                        rv = -1;
                        return rv;
                    }
    
                    char *pszNewPath = getcwd(NULL, 0);
                    printf("%s
    ", pszNewPath);
                    free(pszNewPath);
    
                    struct _finddata_t data;
                    long handle = _findfirst("*.*", &data);
                    if (handle < 0)
                    {
                        return rv;
                    }
                    int nRet = handle < 0 ? -1 : 1;
                    while (nRet >= 0)
                    {
                        if (data.attrib == _A_SUBDIR )
                        {//
                             printf("   [%s]*
    ", data.name ); 
                             
                        }
                        else
                        {
                             printf("   [%s]
    ", data.name );
                        }
                        nRet = _findnext( handle, &data );  
                    }
                    
                    _findclose( handle );     // 关闭当前句柄
                    return rv;
                }
        */
    long nLen = 0;
    int GetFileLength(char *pszPath)
    {
        FILE *pRead = fopen(pszPath, "r");
        if (NULL == pRead)
        {
            return -1;
        }
        char szBuf[1024];
        while (!feof(pRead))
        {
            fgets(szBuf, sizeof(szBuf), pRead);
            nLen++;
        }
    
        fclose(pRead);
        
    }
    
    int SearchPath(char *pszPath)
    {
        int rv = 0;
        rv = chdir(pszPath);
        if (0 != rv)
        {
            printf("func chdir() error
    ");
            rv = -1;
            return rv;
        }
    
        struct _finddata_t data;
        long handle;
        if (-1L == (handle = _findfirst("*.*", &data)))   //成功返回唯一的搜索句柄, 出错返回-1
        {
            return rv;
        }
        do 
        {
            if (data.attrib == _A_SUBDIR )
            {//目录类型
                char szBuf[1024] = {0};
                if (strcmp(data.name, ".") != 0 && strcmp(data.name, "..") != 0)
                {
                    sprintf(szBuf, "%s\%s", pszPath, data.name);
                    SearchPath(szBuf);
                }
            }
            else
            {//单个文件
                int nLen = strlen(data.name);
                if (data.name[nLen - 1] == 'p' && data.name[nLen - 2] == 'p' &&
                    data.name[nLen - 3] == 'c' &&data.name[nLen - 4] == '.' )
                {//过滤出所有cpp的文件
                    printf("   [%s]
    ", data.name );
                    char szBuf[1024] = {0};
                    sprintf(szBuf, "%s\%s", pszPath, data.name);
                    GetFileLength(szBuf);
                }    
            }
        } while(_findnext( handle, &data ) == 0);     //成功返回0 , 出错返回-1
        
        _findclose( handle );     // 关闭当前句柄
        
        return rv;
    }
    
    int main()
    {
        char *pszPath = "C:\Users\SKS\Desktop\谷歌大赛";
        SearchPath(pszPath);
        printf("%ld
    ", nLen);
        return 0;
    }
  • 相关阅读:
    《WF编程》系列之 承载工作流:跟踪服务 Tracking Service
    C#中隐藏(new)和方法重写(override)和重载(overload)的区别
    一套完整自定义工作流的实现
    工作流规范
    新宇面试题
    c# 多态性
    SQL Join连接详解
    Minix安装及配置指南(转载)
    游戏开发的学习资料汇总
    error LNK2019: 无法解析的外部符号 __vsnprintf 问题的解决方法
  • 原文地址:https://www.cnblogs.com/nothx/p/8512353.html
Copyright © 2020-2023  润新知