C 语言递归获取目录下文件

 C_fileGet.c

#include "C_fileGet.h"
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>






C_dirFileSave* C_dirFileSave_INIT()
{
      C_dirFileSave* new_node = (C_dirFileSave*)malloc(sizeof(C_dirFileSave));
      if(new_node == NULL)
      {
            return NULL;
      }

      new_node->next = NULL;
      memset(new_node->fileName,'\0',100);
      return new_node;
}
int C_DIR_read(char*path,C_dirFileSave* head)
{
      if(head == NULL)
            return -1;

      char get_path[1024];
      memset(get_path,'\0',1024);
      strcpy(get_path,path);
      DIR* dir;
      struct dirent* ptr;
      dir = opendir(get_path);
      if(dir == NULL)
      {
            printf("\nopen [%s] failed\n",get_path);
            return -1;
      }
      while((ptr = readdir(dir)) != NULL)
      {
            if(strcmp(ptr->d_name,"..") && strcmp(ptr->d_name,".") && ptr->d_type == 8)
            {
                  C_dirFileSave* new_node = C_dirFileSave_INIT();
                  strcpy(new_node->fileName,ptr->d_name);
                  new_node->type = ptr->d_type;
                  new_node->next = head->next;
                  head->next = new_node;
            }
            if(strcmp(ptr->d_name,"..") && strcmp(ptr->d_name,".") && ptr->d_type == 4)
            {
                  strcat(get_path,"/");
                  strcat(get_path,ptr->d_name);
                  C_DIR_read(get_path,head);
                  memset(get_path,'\0',1024);
                  strcpy(get_path,path);
            }
      }
}
//如果把函数改成这个的话,可以保留文件相对于传入文件夹的路径
int C_DIR_read(char*path,C_dirFileSave* head)
{
      if(head == NULL)
            return -1;

      char get_path[1024];
      memset(get_path,'\0',1024);
      strcpy(get_path,path);
      DIR* dir;
      struct dirent* ptr;
      dir = opendir(get_path);
      if(dir == NULL)
      {
            printf("\nopen [%s] failed\n",get_path);
            return -1;
      }
      while((ptr = readdir(dir)) != NULL)
      {
            if(strcmp(ptr->d_name,"..") && strcmp(ptr->d_name,".") && ptr->d_type == 8)
            {
                  C_dirFileSave* new_node = C_dirFileSave_INIT();
                  strcpy(new_node->fileName,get_path);
                  strcat(new_node->fileName,"/");
                  strcat(new_node->fileName,ptr->d_name);
                  new_node->type = ptr->d_type;
                  new_node->next = head->next;
                  head->next = new_node;
            }
            if(strcmp(ptr->d_name,"..") && strcmp(ptr->d_name,".") && ptr->d_type == 4)
            {
                  strcat(get_path,"/");
                  strcat(get_path,ptr->d_name);
                  C_DIR_read(get_path,head);
                  memset(get_path,'\0',1024);
                  strcpy(get_path,path);
            }
      }
}

 C_fileGet.h

#ifndef _CONE_LY_C_FILE_GET_H
#define _CONE_LY_C_FILE_GET_H



typedef struct _C_DIR_FILE_SAVE{
      int type;
      char fileName[100];
      struct _C_DIR_FILE_SAVE* next;
}C_dirFileSave;

C_dirFileSave* C_dirFileSave_INIT();
int C_DIR_read(char*path,C_dirFileSave* head);






#endif/*_CONE_LY_C_FILE_GET_H*/

main.c

#include "C_fileGet.h"
#include <stdio.h>

int main(int _INPUT_NUMBER,char**_INPUT_STRING)
{
      C_dirFileSave*  head = C_dirFileSave_INIT();
      C_DIR_read(*(_INPUT_STRING+1),head);
      C_dirFileSave* get_head = head->next;


      for(;get_head!=NULL;get_head = get_head->next)
      {
            printf("file:[%s]\n",get_head->fileName);
      }
}

以后可能会用的上,记录一份

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值