1. 程式人生 > >linux C語言遍歷資料夾所有檔案

linux C語言遍歷資料夾所有檔案

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
void visit(const char *);
int main(){
	visit(".");   // 指定資料夾
	return 0;
}
void visit(const char * path){
    dirent * dirt;// linux 下的結構體
    DIR * dir;;
    char *s[100];
    int i=0,n=1;
    s[0]= new  char[50];
    memset(s[0],0,50);
    sprintf(s[0],"%s",path);
    do{
        dir = opendir(s[i]);// 開啟指定路徑
        if(dir!=NULL)
            while((dirt=readdir(dir))!=NULL){     // 儲存路徑下的所有檔案和資料夾路徑
                if(strcmp(dirt->d_name,".")==0||strcmp(dirt->d_name,"..")==0)
                    continue;
                s[n]= new  char[50];
                memset(s[n],0,50);
                sprintf(s[n],"%s/%s",s[i],dirt->d_name);
                n++;
            }
        i++;
    }while(i<n);
    for(i=0;i<n;i++)     // 廣度優先遍歷資料夾
        printf("%s\n",s[i]);
    closedir(dir);
    while(n--){
        //remove(s[n]);
        delete []s[n];
    }
}

基於Qt QString 的遞迴遍歷檔案

QString是qt系統整合的一個類,可以自行用C++實現,通過過載運算子和構造方法就可以實現

void visit(const char * path){
    dirent * dirt;
    DIR * dir;
    if((dir= opendir(path) ) != NULL){
        while((dirt=readdir(dir))){
            printf("%s/%s\n",path,dirt->d_name);
            if(strcmp(dirt->d_name,".")&strcmp(dirt->d_name,"..")){
                visit(QString(QString(path)+"/"+QString(dirt->d_name)).toUtf8().toStdString().data());
            }
        }
    }
    closedir(dir);
}


相關推薦

linux C語言資料所有檔案

#include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> #include <stdio.h> voi

php資料所有檔案

function listFiles($dir) { $files = array(); $handle = opendir($dir); while ($file = readdir($handle)) { if (

VC 資料所有檔名,程式碼簡短。下面函式是獲取YY賬號,(對xml的解析操作)

#include <filesystem> #include <string> #include <vector> #include "tinyxml.h" using namespace std; using namespace std:

C/C++資料檔案

庫函式 包含標頭檔案 #include 用到資料結構_finddata_t,檔案資訊結構體的指標。 struct _finddata_t { unsigned attrib; //檔案屬性 time_t time_c

C#資料下子檔案

 public  class ChooseMove     {         //遍歷資料夾         public List<string> FindFile

php資料檔案

php遍歷資料夾、檔案 <?php $path = '/'; foreach_file($path);//傳入需要遍歷的資料夾路徑 function foreach_file($path) { if(is_dir($path))//判斷目錄是否存在 {

python使用os.walk和os.path.join來資料檔案(包括子資料下的檔案)

使用os.walk和os.path.join來遍歷資料夾的檔案 import os import os.path path = 'C://' for root, dirs, files in os.walk(path):    for file in files:  

VBA資料檔案檔案實用原始碼

‘批量遍歷資料夾下某類檔案,並統計編號 Sub OpenAndClose() Dim MyFile As String Dim s As String Dim count As Integer MyFile = Dir("d:\data\" & "*.csv")

shell 遞迴資料檔案行首和末尾進行修改

功能:    遞迴遍歷資料夾下的cs檔案或者lua檔案,並對檔案的行首或者末尾插入程式碼#!/bin/bash str="" luafile=false csfile=false dfs(){ #echo $1 for file in $1/* do if [ -

Qt總結之二:資料檔案目錄,並過濾和獲取檔案資訊、字尾名、字首名(二)

前言 需要在特定目錄或磁碟下查詢特定檔案 一、篩選目錄 (一)單一目錄下遍歷,篩選特定檔案 QDir dir("./SaveFiles"); QFileInfoList list = dir.entryInfoList(); (二)裝置所有磁碟中遍歷 QF

PHP遞迴和非遞迴資料檔案

function readDirFiles($dir){ $files= []; $queue=[realpath($dir)]; $currentPath = current($queue); while($currentPath) {

Qt總結之一:資料檔案目錄,並過濾和獲取檔案資訊、字尾名、字首名(一)

一、採用遞迴和QDir實現資料夾下所有檔案遍歷的方法 #include <QDir> bool FindFile(const QString & path) {     QDir dir(path);   if (!dir.exists(

Qt總結之三:磁碟檔案操作、資料檔案目錄,並過濾和獲取檔案資訊、字尾名、字首名(三)

前言 本節內容主要包括磁碟容量檢測、磁碟內指定或特定檔案的操作 話不多說,先上效果圖 共分為兩個部分,第一部分是檢測磁碟容量,第二部分是篩選磁碟內指定檔案(test.txt)或特定檔案(.txt / .png型別檔案) 獲取磁碟容量關鍵函式:【fileapi.h】 

Qt實現資料檔案目錄,並過濾和獲取檔案資訊、字尾名、字首名(三)

下面是自己的實際工作中寫的程式碼,請大家斧正#ifndefINQUIRYDIALOG_H#defineINQUIRYDIALOG_H#include<QDialog>#include<QFileDialog>#include<QDir>#i

python資料檔案

在Python中,檔案操作主要來自os模組,主要方法如下: os.listdir(dirname):列出dirname下的目錄和檔案 os.getcwd():獲得當前工作目錄 os.curdir:返回當前目錄('.') os.chdir(dirname):改變工作目錄到di

多執行緒例項——資料分割檔案識別檔案內容

需求:遍歷資料夾下的所有pdf檔案,對每個pdf檔案根據二維碼進行分割,再對分割後的檔案的內容進行識別。 可以拆分為以下幾個關鍵方法: 1.GetFileList方法:遍歷檔案,獲取原始檔動態陣列(這裡假設3個資料夾,每個資料夾下有3個檔案,則原始檔個數為9),耗時忽略不計 1 static List&

linuxc語言遞迴資料所有檔案和子資料(附上替換文字檔案內容的方法)

#include <stdio.h> #include <sys/dir.h> #include <string> #include <sys/stat.h> //判斷是否為資料夾 bool isDir(const cha

c#資料獲得所有檔案

c#遍歷資料夾獲得所有檔案 在c#中,想要獲得一個資料夾下的所有子目錄以及檔案十分簡單。 首先,獲取目錄的情況下,DirectoryInfo.GetDirectories():獲取目錄(不包含子目錄)的子目錄,返回型別為DirectoryInfo[],支援萬用字元查詢; 其次,獲取檔案的

C++資料下所有的wav檔案(支援windows和Linux

直接上程式碼: #include <iostream> #include <string> #include <vector> #include <fstream> #include <string.h> #includ

linux迴圈資料所有檔案

demofun(){ for file in `ls $1` do if test -f $file then echo "file: $file" elif test -d $file then