程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 【app】遍歷目錄所有文件,app歷目錄

【app】遍歷目錄所有文件,app歷目錄

編輯:關於C語言

【app】遍歷目錄所有文件,app歷目錄


遍歷目錄所有文件

 

  原創,轉載時請注明,謝謝。郵箱:[email protected],  地址:http://www.cnblogs.com/embedded-tzp

    

 

 

 #include <stdio.h> 
    #include <dirent.h>
    #include <stdlib.h>

    int main(){
          DIR *dir_p = opendir("/"); 
          if(dir_p == NULL) perror("opendir"), exit(-1);
          struct dirent *ent;
          while(1){
                ent = readdir(dir_p);
                if(ent == NULL)  break;
              //打印子項類型和子項名
                if( 0 == strcmp(ent->d_name, ".") 
                 || 0 == strcmp(ent->d_name, "..")){
                        continue;
              }      
                printf("%d, %s\n", ent->d_type, ent->d_name);
                //type == 4 是目錄,其他是文件
          }
}

 

 

 

 

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int dir_printall(const char *pathname)
{
    struct dirent **namelist = NULL;
    int ent_n;
    int i;

    ent_n = scandir(pathname, &namelist, NULL, alphasort);
    if (ent_n < 0){
        printf("scandir() fail : %s\n", pathname);
        return -1;
    }
    for(i = 0; i < ent_n; i++){
        if( 0 == strcmp(namelist[i]->d_name, ".")
                || 0 == strcmp(namelist[i]->d_name, "..")){ // skip parent dir and self
            continue;
        }
        
        char path_buf[512] = {0}; // use malloc will be bettor  
        sprintf(path_buf, "%s/%s", pathname, namelist[i]->d_name);

        printf("%s\n", path_buf);

        if(4 == namelist[i]->d_type){ // 4 means dir
            int retval = dir_printall( path_buf); // recurrence call
            if(-1 == retval){
                fprintf(stderr, "dir_printall() fail: %s\n", path_buf);
                continue;
                //goto out; // not end, for /proc/5236/net can't
            }
        }
    }
     

out:
    for(i = 0; i < ent_n; i++){
        if(namelist[i]){
            free(namelist[i]);
            namelist[i] = NULL;
        }
    }
    if(namelist){
        free(namelist);
        namelist = NULL;
    }
    return 0;

}
int main(void)
{
    if (-1 == dir_printall("/")){
        perror("dir_printall()");
    }
    return 0;
}

 

 

    

      

#帶完整路徑

file=$(find ./)

echo $file

#只有文件名

file=$(ls -R)

echo $file

 

自己做的項目中拷貝出來的一段簡化的代碼。

bool SearchThread::createDb()
{   
 qDebug() << __LINE__ << __FUNCTION__;
        QFileInfoList fileDriveList = QDir::drives(); // 獲取盤符,windows下為c:, d:, e:, linux下為 /
        foreach(QFileInfo fileDrive, fileDriveList){ // 循環處理盤符
                qDebug() << fileDrive.absoluteFilePath();
                    createDb(fileDrive.absoluteFilePath());
        }    
        return true;
}

bool SearchThread::createDb(const QString &filePath)
{
        QDir dir = filePath;

const QDir::Filters FILTERS =  QDir::AllDirs | QDir::Files | QDir::Drives
                              | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System;
        QFileInfoList fileInfoList = dir.entryInfoList(FILTERS, QDir::DirsFirst | QDir::Name);
        foreach(QFileInfo fileInfo, fileInfoList){
                bool isdir = fileInfo.isDir();    
                if(isdir){
                        if(!fileInfo.isSymLink()){ // 不是鏈接文件,防止死循環
                            createDb(fileInfo.absoluteFilePath());
                        }
                }
        }
    return true;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved