深入探討:linux中遍曆檔案夾下的所有檔案

來源:互聯網
上載者:User

linux C 遍曆目錄及其子目錄
複製代碼 代碼如下:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
void listDir(char *path)
{
DIR *pDir ;
struct dirent *ent ;
int i=0 ;
char childpath[512];

pDir=opendir(path);
memset(childpath,0,sizeof(childpath));

while((ent=readdir(pDir))!=NULL)
{

if(ent->d_type & DT_DIR)
{

if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
continue;

sprintf(childpath,"%s/%s",path,ent->d_name);
printf("path:%s/n",childpath);

listDir(childpath);

}
else
{
cout<<ent->d_name<<endl;
}
}

}

int main(int argc,char *argv[])
{
listDir(argv[1]);
return 0;
}

Linux C :遍曆輸出指定目錄下的所有檔案
在Linux下opendir()、readdir()和closedir()這三個函數主要用來遍曆目錄。在使用這三個函數前必須先包括以下兩個標頭檔:
#include <sys/types.h>
#include <dirent.h>
opendir函數的原型為:
DIR *opendir(const char *name);
它返回一個DIR*類型,這就是一個控制代碼啦,你不用管它的內部結構是什麼樣的,只要知道這個控制代碼就是等一下要傳給readdir()函數的參數就行了。
readdir函數的原型為:
struct dirent *readdir(DIR *dir);
看它的參數就知道該參數是opendir函數返回的控制代碼,而該函數的傳回值是struct dirent* 類型,這裡我們必須瞭解一下這個結構體:複製代碼 代碼如下:struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};

這個結構體的d_name存放的就是檔案的名字,這裡的檔案包括普通檔案,目錄檔案等等,在linux的思想中,所有的東西都是檔案。
closedir函數的原型為:
int closedir(DIR *dir);
這個函數就不用多說了,一般有開(open),就有關(close),這樣的結構經常可出看到,如fopen,fclose等等。
三個函數介紹完了,直接來一個例子吧:
**********************************SearchDir.c****************************複製代碼 代碼如下:#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
char filename[256][256];
int len = 0;
int trave_dir(char* path, int depth)
{
DIR *d; //聲明一個控制代碼
struct dirent *file; //readdir函數的傳回值就存放在這個結構體中
struct stat sb;

if(!(d = opendir(path)))
{
printf("error opendir %s!!!\n",path);
return -1;
}
while((file = readdir(d)) != NULL)
{
//把目前的目錄.,上一級目錄..及隱藏檔案都去掉,避免死迴圈遍曆目錄
if(strncmp(file->d_name, ".", 1) == 0)
continue;
strcpy(filename[len++], file->d_name); //儲存遍曆到的檔案名稱
//判斷該檔案是否是目錄,及是否已搜尋了三層,這裡我定義只搜尋了三層目錄,太深就不搜了,省得搜出太多檔案
if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth <= 3)
{
trave_dir(file->d_name, depth + 1);
}
}
closedir(d);
return 0;
}
int main()
{
int depth = 1;
int i;
trave_dir("/usr/keygoe/ini/", depth);
for(i = 0; i < len; i++)
{
printf("%s\t", filename[i]);
}
printf("\n");
return 0;
}

Linux下C語言遍曆檔案夾
學習了LINUX下用C語言遍曆檔案夾,一些心得
struct dirent中的幾個成員:
d_type:4表示為目錄,8表示為檔案
d_reclen:16表示子目錄或檔案,24表示非子目錄
經過本人親自實驗發現:d_reclen:16表示子目錄或以.開頭的隱藏檔案,24表示普通文字檔,28為二進位檔案,等等
d_name:目錄或檔案的名稱
具體代碼如下,僅供參考複製代碼 代碼如下:#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
void List(char *path)
{
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while (NULL != (ent=readdir(pDir)))
{
if (ent->d_reclen==24)
{
if (ent->d_type==8)
{
printf("普通檔案:%s\n", ent->d_name);
}
else
{
printf("子目錄:%s\n",ent->d_name);
List(ent->d_name);
printf("返回%s\n",ent->d_name);
}
}
}
}
int main(int argc, char *argv[])
{
List(argv[1]);
return 0;
}

上面函數修改後:複製代碼 代碼如下:void List(char *path)
{
printf("路徑為[%s]\n", path);

struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
//d_reclen:16表示子目錄或以.開頭的隱藏檔案,24表示普通文字檔,28為二進位檔案,還有其他……
while (NULL != (ent=readdir(pDir)))
{
printf("reclen=%d type=%d\t", ent->d_reclen, ent->d_type);
if (ent->d_reclen==24)
{
//d_type:4表示為目錄,8表示為檔案
if (ent->d_type==8)
{
printf("普通檔案[%s]\n", ent->d_name);
}
}
else if(ent->d_reclen==16)
{
printf("[.]開頭的子目錄或隱藏檔案[%s]\n",ent->d_name);
}
else
{
printf("其他檔案[%s]\n", ent->d_name);
}
}
}

複製代碼 代碼如下:#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>

void dir_scan(char *path, char *file);
int count = 0;

int main(int argc, char *argv[])
{
struct stat s;

if(argc != 2){
printf("one direction requried\n");
exit(1);
}
if(lstat(argv[1], &s) < 0){
printf("lstat error\n");
exit(2);
}
//判斷一個路徑是否是目錄
if(!S_ISDIR(s.st_mode)){
printf("%s is not a direction name\n", argv[1]);
exit(3);
}

dir_scan("", argv[1]);

printf("total: %d files\n", count);

exit(0);
}

void dir_scan(char *path, char *file)
{
struct stat s;
DIR *dir;
struct dirent *dt;
char dirname[50];

memset(dirname, 0, 50*sizeof(char));
strcpy(dirname, path);

if(lstat(file, &s) < 0){
printf("lstat error\n");
}

if(S_ISDIR(s.st_mode)){
strcpy(dirname+strlen(dirname), file);
strcpy(dirname+strlen(dirname), "/");
if((dir = opendir(file)) == NULL){
printf("opendir %s/%s error\n");
exit(4);
}
if(chdir(file) < 0) {
printf("chdir error\n");
exit(5);
}
while((dt = readdir(dir)) != NULL){
if(dt->d_name[0] == '.'){
continue;
}

dir_scan(dirname, dt->d_name);
}
if(chdir("..") < 0){
printf("chdir error\n");
exit(6);
}
}else{
printf("%s%s\n", dirname, file);
count++;
}
}

linux c 下如何獲得目錄下的檔案數目。
複製代碼 代碼如下:int main(int argc, char **argv)
{
DIR * pdir;
struct dirent * pdirent;
struct stat f_ftime;
int fcnt;/*檔案數目統計*/
pdir=opendir("./");
if(pdir==NULL)
{ return(-1); }
fcnt=0;
for(pdirent=readdir(pdir);pdirent!=NULL;pdirent=readdir(pdir))
{
if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0) continue;
if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
if(S_ISDIR(f_ftime.st_mode)) continue; /*子目錄跳過*/
fcnt++;
printf("檔案:%s\n",pdirent->d_name);
}
printf("檔案總數%d\n",fcnt);
closedir(pdir);
return 0;
}
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;

if((dp = opendir(dir)) == NULL) {
fprintf(stderr, "cannot open directory: %s\n ", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry-> d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/**//* Found a directory, but ignore . and .. */
if(strcmp( ". ",entry-> d_name) == 0 ||
strcmp( ".. ",entry-> d_name) == 0)
continue;
printf( "%*s%s/\n ",depth, " ",entry-> d_name);
/**//* Recurse at a new indent level */
printdir(entry-> d_name,depth+4);
}
else printf( "%*s%s\n ",depth, " ",entry-> d_name);
}
chdir( ".. ");
closedir(dp);
}

/**//* Now we move onto the main function. */

int main(int argc, char* argv[])
{
char *topdir, pwd[2]= ". ";
if (argc != 2)
topdir=pwd;
else
topdir=argv[1];

printf( "Directory scan of %s\n ",topdir);
printdir(topdir,0);
printf( "done.\n ");

exit(0);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.