LinuxC读取⽂件夹下所有⽂件(包括⼦⽂件夹)的⽂件名Linux C 下⾯读取⽂件夹要⽤到结构体struct dirent,在头#include <dirent.h>中,如下:
#include <dirent.h>
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在⽬录⽂件中的偏移 */
unsigned short d_reclen; /* length of this d_name ⽂件名长 */
unsigned char d_type; /* the type of d_name ⽂件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) ⽂件名,最长255字符 */
}
其中d_type表明该⽂件的类型:⽂件(8)、⽬录(4)、链接⽂件(10)等。
下⾯程序,递归读取某⽂件夹及其⼦⽂件夹下所有⽂件名:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 int readFileList(char *basePath)
7 {
8 DIR *dir;
9 struct dirent *ptr;
10 char base[1000];
11
12 if ((dir=opendir(basePath)) == NULL)
13 {
14 perror("Open ");
15 exit(1);
16 }
17
18 while ((ptr=readdir(dir)) != NULL)
19 {
20 if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir
21 continue;
22 else if(ptr->d_type == 8) ///file
23 printf("d_name:%s/%s\n",basePath,ptr->d_name);
24 else if(ptr->d_type == 10) ///link file
25 printf("d_name:%s/%s\n",basePath,ptr->d_name);
26 else if(ptr->d_type == 4) ///dir
27 {
28 memset(base,'\0',sizeof(base));
29 strcpy(base,basePath);
30 strcat(base,"/");
31 strcat(base,ptr->d_name);
32 readFileList(base);
33 }
34 }
35 closedir(dir);
36 return 1;
37 }
38
39 int main(void)
40 {
41 DIR *dir;
42 char basePath[1000];
43
44 ///get the current absoulte path
45 memset(basePath,'\0',sizeof(basePath));
46 getcwd(basePath, 999);
47 printf("the current dir is : %s\n",basePath);
48
49 ///get the file list
50 memset(basePath,'\0',sizeof(basePath));
51 strcpy(basePath,"./XL");
52 readFileList(basePath);
53 return 0;
54 }
执⾏输出:
====================下⾯是脚本之家========================
深⼊探讨:linux中遍历⽂件夹下的所有⽂件
linux C 遍历⽬录及其⼦⽬录
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <dirent.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8using namespace std;
9void listDir(char *path)
10 {
11 DIR *pDir ;
12struct dirent *ent ;
13int i=0 ;
14char childpath[512];
15
16 pDir=opendir(path);
17 memset(childpath,0,sizeof(childpath));
18
19
20while((ent=readdir(pDir))!=NULL)
21 {
22
23if(ent->d_type & DT_DIR)
24 {
25
26if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
27continue;
28
29 sprintf(childpath,"%s/%s",path,ent->d_name);
30 printf("path:%s/n",childpath);
31
32 listDir(childpath);
33
34 }
35else
36 {
37 cout<<ent->d_name<<endl;
38 }
39 }
40
41 }
42
43int main(int argc,char *argv[])
44 {
45 listDir(argv[1]);
46return0;
47 }
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****************************
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <dirent.h>
5 #include <sys/stat.h>
6char filename[256][256];
7int len = 0;
8int trave_dir(char* path, int depth)
9 {
10 DIR *d; //声明⼀个句柄
11struct dirent *file; //readdir函数的返回值就存放在这个结构体中
12struct stat sb;
13
14if(!(d = opendir(path)))
15 {
16 printf("error opendir %s\n",path);
17return -1;
18 }
19while((file = readdir(d)) != NULL)
20 {
21//把当前⽬录.,上⼀级⽬录..及隐藏⽂件都去掉,避免死循环遍历⽬录
22if(strncmp(file->d_name, ".", 1) == 0)
23continue;
24 strcpy(filename[len++], file->d_name); //保存遍历到的⽂件名
25//判断该⽂件是否是⽬录,及是否已搜索了三层,这⾥我定义只搜索了三层⽬录,太深就不搜了,省得搜出太多⽂件
26if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth <= 3)
27 {
28 trave_dir(file->d_name, depth + 1);
29 }
30 }
31 closedir(d);
32return0;
33 }
34int main()
35 {
36int depth = 1;
37int i;
38 trave_dir("/usr/keygoe/ini/", depth);
39for(i = 0; i < len; i++)
40 {
41 printf("%s\t", filename[i]);
42 }
43 printf("\n");
44return0;
45 }
Linux下C语⾔遍历⽂件夹
学习了LINUX下⽤C语⾔遍历⽂件夹,⼀些⼼得
struct dirent中的⼏个成员:
d_type:4表⽰为⽬录,8表⽰为⽂件
d_reclen:16表⽰⼦⽬录或⽂件,24表⽰⾮⼦⽬录
经过本⼈亲⾃试验发现:d_reclen:16表⽰⼦⽬录或以.开头的隐藏⽂件,24表⽰普通⽂本⽂件,28为⼆进制⽂件,等等d_name:⽬录或⽂件的名称
具体代码如下,仅供参考
1 #include <stdio.h>
2 #include <dirent.h>
3 #include <sys/stat.h>
4void List(char *path)
5 {
6struct dirent* ent = NULL;
7 DIR *pDir;
8 pDir=opendir(path);
9while (NULL != (ent=readdir(pDir)))
10 {
11if (ent->d_reclen==24)
12 {
13if (ent->d_type==8)
14 {
15 printf("普通⽂件:%s\n", ent->d_name);
16 }
17else
18 {
19 printf("⼦⽬录:%s\n",ent->d_name);
20 List(ent->d_name);
21 printf("返回%s\n",ent->d_name);
22 }
23 }
24 }
25 }
26int main(int argc, char *argv[])
27 {
28 List(argv[1]);
29return0;
30 }
上⾯函数修改后:
1void List(char *path)
2 {
3 printf("路径为[%s]\n", path);
4
5struct dirent* ent = NULL;
6 DIR *pDir;
7 pDir=opendir(path);
8//d_reclen:16表⽰⼦⽬录或以.开头的隐藏⽂件,24表⽰普通⽂本⽂件,28为⼆进制⽂件,还有其他…… 9while (NULL != (ent=readdir(pDir)))
10 {
11 printf("reclen=%d type=%d\t", ent->d_reclen, ent->d_type);
12if (ent->d_reclen==24)
13 {
14//d_type:4表⽰为⽬录,8表⽰为⽂件
15if (ent->d_type==8)
16 {
17 printf("普通⽂件[%s]\n", ent->d_name);
18 }
19 }
20else if(ent->d_reclen==16)
21 {
22 printf("[.]开头的⼦⽬录或隐藏⽂件[%s]\n",ent->d_name);
23 }
24else
25 {
26 printf("其他⽂件[%s]\n", ent->d_name);
27 }
28 }
29 }
1 #include <stdio.h>
2 #include <dirent.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5
6void dir_scan(char *path, char *file);
7int count = 0;
8
9int main(int argc, char *argv[])
10 {
11struct stat s;
12
13if(argc != 2){
14 printf("one direction requried\n");
15 exit(1);
16 }
17if(lstat(argv[1], &s) < 0){
18 printf("lstat error\n");
19 exit(2);
20 }
21//判断⼀个路径是否是⽬录
22if(!S_ISDIR(s.st_mode)){
23 printf("%s is not a direction name\n", argv[1]);
24 exit(3);
25 }
26
27 dir_scan("", argv[1]);
28
29 printf("total: %d files\n", count);
30
31 exit(0);
32 }
33
34void dir_scan(char *path, char *file)
35 {
36struct stat s;
37 DIR *dir;
38struct dirent *dt;
39char dirname[50];
40
41 memset(dirname, 0, 50*sizeof(char));
42 strcpy(dirname, path);
43
44if(lstat(file, &s) < 0){linux查看当前文件夹内容
45 printf("lstat error\n");
46 }
47
48if(S_ISDIR(s.st_mode)){
49 strcpy(dirname+strlen(dirname), file);
50 strcpy(dirname+strlen(dirname), "/");
51if((dir = opendir(file)) == NULL){
52 printf("opendir %s/%s error\n");
53 exit(4);
54 }
55if(chdir(file) < 0) {
56 printf("chdir error\n");
57 exit(5);
58 }
59while((dt = readdir(dir)) != NULL){
60if(dt->d_name[0] == '.'){
61continue;
62 }
63
64 dir_scan(dirname, dt->d_name);
65 }
66if(chdir("..") < 0){
67 printf("chdir error\n");
68 exit(6);
69 }
70 }else{
71 printf("%s%s\n", dirname, file);
72 count++;
73 }
74 }
linux c 下如何获得⽬录下的⽂件数⽬。
1int main(int argc, char **argv)
2 {
3 DIR * pdir;
4struct dirent * pdirent;
5struct stat f_ftime;
6int fcnt;/*⽂件数⽬统计*/
7 pdir=opendir("./");
8if(pdir==NULL)
9 { return(-1); }
10 fcnt=0;
11for(pdirent=readdir(pdir);pdirent!=NULL;pdirent=readdir(pdir))
12 {
13if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0) continue; 14if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
15if(S_ISDIR(f_ftime.st_mode)) continue; /*⼦⽬录跳过*/
16 fcnt++;
17 printf("⽂件:%s\n",pdirent->d_name);
18 }
19 printf("⽂件总数%d\n",fcnt);
20 closedir(pdir);
21return0;
22 }
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <dirent.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29void printdir(char *dir, int depth)
30 {
31 DIR *dp;
32struct dirent *entry;
33struct stat statbuf;
34
35if((dp = opendir(dir)) == NULL) {
36 fprintf(stderr, "cannot open directory: %s\n ", dir);
37return;
38 }
39 chdir(dir);
40while((entry = readdir(dp)) != NULL) {
41 lstat(entry-> d_name,&statbuf);
42if(S_ISDIR(statbuf.st_mode)) {
43/**//* Found a directory, but ignore . and .. */
44if(strcmp( ". ",entry-> d_name) == 0 ||
45 strcmp( ".. ",entry-> d_name) == 0)
46continue;
47 printf( "%*s%s/\n ",depth, "",entry-> d_name);
48/**//* Recurse at a new indent level */
49 printdir(entry-> d_name,depth+4);
50 }
51else printf( "%*s%s\n ",depth, "",entry-> d_name);
52 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论