Linux C 读取文件夹下所有文件(包括子文件夹)的文件名

简介: 本文:http://www.cnblogs.com/xudong-bupt/p/3504442.html Linux C  下面读取文件夹要用到结构体struct dirent,在头#include 中,如下: #include struct dirent { long d_i...

本文:http://www.cnblogs.com/xudong-bupt/p/3504442.html

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 dir error...");
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 }

执行输出 :

相关文章
|
9月前
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
|
7月前
|
存储 数据管理 Linux
区分Linux中.tar文件与.tar.gz文件的不同。
总之,".tar"文件提供了一种方便的文件整理方式,其归档但不压缩的特点适用于快速打包和解压,而".tar.gz"文件通过额外的压缩步骤,尽管处理时间更长,但可以减小文件尺寸,更适合于需要节约存储空间或进行文件传输的场景。用户在选择时应根据具体需求,考虑两种格式各自的优劣。
1174 13
|
8月前
|
安全 Linux
Linux赋予文件000权限的恢复技巧
以上这些步骤就像是打开一扇锁住的门,步骤看似简单,但是背后却有着严格的逻辑和规则。切记,在任何时候,变更文件权限都要考虑安全性,不要无谓地放宽权限,那样可能
256 16
|
9月前
|
Linux
【Linux】 Linux文件I/O常见操作技巧
以上就是Linux文件I/O操作的一些技巧,接纳它们,让它们成为你在Linux世界中的得力伙伴,工作会变得轻松许多。不过记住,技巧的运用也需要根据实际情况灵活掌握,毕竟,最适合的才是最好的。
276 28
|
8月前
|
存储 Linux 数据处理
深入剖析Linux中一切即文件的哲学和重定向的机制
在计算机的奇妙世界中,Linux的这套哲学和机制减少了不同类型资源的处理方式,简化了抽象的概念,并蕴藏着强大的灵活性。就像变戏法一样,轻轻松松地在文件、程序与设备之间转换数据流,标准输入、输出、错误流就在指尖舞动,程序的交互和数据处理因此变得既高效又富有乐趣。
147 4
|
9月前
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
1906 12
|
8月前
|
Linux
linux文件重命名命令
本指南介绍Linux文件重命名方法,包括单文件操作的`mv`命令和批量处理的`rename`命令。`mv`可简单更改文件名并保留扩展名,如`mv old_file.txt new_name.txt`;`rename`支持正则表达式,适用于复杂批量操作,如`rename &#39;s/2023/2024/&#39; *.log`。提供实用技巧如大小写转换、数字序列处理等,并提醒覆盖风险与版本差异,建议使用`-n`参数预览效果。
Linux_学习_01_ 压缩文件夹
      二、参考资料 1.Linux下压缩某个文件夹命令
776 0
|
Linux 数据安全/隐私保护
Linux 压缩(打包)文件夹 tar/zip
tar 压缩方法: tar -zcvf /home/xahot.tar.gz /xahot tar -zcvf 打包后生成的文件名全路径 要打包的目录 例子:把/xahot文件夹打包后生成一个/home/xahot.tar.gz的文件。zip 压缩方法: 压缩当前的文件夹 zip -r ./xahot.zip ./* -r表示递归 zip [参数] [打包后的文件名]
24806 0