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、\BC\LIB等),文件名name(TC、WPS等),
擴展名ext(.EXE、.COM等),並分別存入相應的變量中.  

====================================================
void fumerge(char *path,char *drive,char *dir,char *name,char *ext)  
此函數通過盤符drive(C:、A:等),路徑dir(\TC、\BC\LIB等),文件名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\n", szBuf);
                }

                int rv = 0;
                rv = chdir("C:\\Users\\SKS\\Desktop\\0114文件操作");
                if (0 != rv)
                {
                    printf("func chdir() error\n");
                    rv = -1;
                    return rv;
                }

                char *pszNewPath = getcwd(NULL, 0);
                printf("%s\n", 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]*\n", data.name ); 
                         
                    }
                    else
                    {
                         printf("   [%s]\n", 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\n");
        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]\n", 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\n", nLen);
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM