C语言文件夹遍历

遍历当前文件夹

## 遍历指定文件夹中所有的文件,排除.和..两种伪文件外,并获取文件名称、后缀名、不带后缀的文件名。
```c
int TraverseDir(const char * szFilePath )
{

        DIR * pDir = opendir(szFilePath);
        if(pDir)
        {
                printf("[%s]\n", szFilePath);

        }
        else
        {
                printf("open dir [%s] ,failed.\n", szFilePath);
                return -1;
        }
        struct dirent *pDirent;

        while((pDirent = readdir(pDir)) != nullptr)
        {
                //printf("[FileName:%32s],\033[4;31;40m Type[%d] \033[0m \n", pDirent->d_name, pDirent->d_type);
                if(strcmp(".", pDirent->d_name) == 0 || 0 == strcmp("..", pDirent->d_name) )
                {
                        continue;
                }

                char *pTemp = strchr(pDirent->d_name, '.');
                if(nullptr != pTemp)
                {
                        printf("[FileName:%16s],\033[4;31;40m Type[%d]\033[0m,[Suffix:\t%16s]; basename[%16s],dirname[%32s] ",pDirent->d_name, pDirent->d_type, pTemp, basename(pDirent->d_name), dirname(pDirent->d_name));
                }
                char szFileName[128]={0};
                if(pTemp != nullptr )
                {
                        strncpy(szFileName, pDirent->d_name, pTemp - pDirent->d_name);
                        printf("final file name[%16s]\n", szFileName);

                }

        }

        closedir(pDir);

        return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用C语言中的文件操作函数来遍历多个文件夹内的文件。具体步骤如下: 1. 使用 opendir 函数打开需要遍历文件夹,获取文件夹的指针。 2. 使用 readdir 函数读取文件夹中的文件,获取文件的指针。 3. 对于每个文件,可以使用 stat 函数获取文件的信息,如文件名、文件大小、修改时间等。 4. 如果当前文件是一个文件夹,可以使用递归的方式进入该文件夹,重复步骤 2-4。 5. 使用 closedir 函数关闭打开的文件夹指针。 下面是一个简单的示例代码,可以遍历指定文件夹内的所有文件: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <dirent.h> void traverse_dir(const char *path) { DIR *dir; struct dirent *entry; struct stat info; if ((dir = opendir(path)) == NULL) { perror("opendir"); return; } while ((entry = readdir(dir)) != NULL) { char full_path[1024]; sprintf(full_path, "%s/%s", path, entry->d_name); if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } if (stat(full_path, &info) != 0) { perror("stat"); continue; } if (S_ISDIR(info.st_mode)) { traverse_dir(full_path); } else { printf("%s\n", full_path); } } closedir(dir); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <directory>\n", argv[0]); exit(EXIT_FAILURE); } traverse_dir(argv[1]); return 0; } ``` 使用方式为:`./a.out <directory>`,其中 `<directory>` 是需要遍历文件夹路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪里淘沙小丸子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值