日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux中的读目录,在linux中读取目录内容

發布時間:2025/3/21 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux中的读目录,在linux中读取目录内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們如何在

Linux中使用C語言讀取目錄的內容(子目錄和文件名).

這是一個遞歸程序,以遞歸方式打印所有子目錄和文件的名稱.

用法:./ a.out路徑名

不檢查作為命令行參數提供的初始路徑名的錯誤條件.

基本代碼流程:

讀取當前目錄中的所有條目.

如果是目錄名,則將其名稱添加到路徑名中,并以遞歸方式調用函數.

否則打印文件的名稱.

有關特定函數的詳細信息可以在dmuir指出的各個手冊頁中引用:

#include

#include

#include

#include

int read(char *pth)

{

char path[1000];

strcpy(path,pth);

DIR *dp;

struct dirent *files;

/*structure for storing inode numbers and files in dir

struct dirent

{

ino_t d_ino;

char d_name[NAME_MAX+1]

}

*/

if((dp=opendir(path))==NULL)

perror("dir\n");

char newp[1000];

struct stat buf;

while((files=readdir(dp))!=NULL)

{

if(!strcmp(files->d_name,".") || !strcmp(files->d_name,".."))

continue;

strcpy(newp,path);

strcat(newp,"/");

strcat(newp,files->d_name);

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

//stat function return a structure of information about the file

if(stat(newp,&buf)==-1)

perror("stat");

if(S_ISDIR(buf.st_mode))// if directory, then add a "/" to current path

{

strcat(path,"/");

strcat(path,files->d_name);

read(path);

strcpy(path,pth);

}

}

}

int main(int argc,char *argv[])

{

read(argv[1]);

}

總結

以上是生活随笔為你收集整理的linux中的读目录,在linux中读取目录内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。