该文章为递归寻找目录下目标文件(待完善,但是能用)
生活随笔
收集整理的這篇文章主要介紹了
该文章为递归寻找目录下目标文件(待完善,但是能用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先是鏈表,目前設置為雙向鏈表還未簡化.
頭文件bothlist.h
#ifndef __BOTHLIST_H__ #define __BOTHLIST_H__struct list_head *create_list_head(void); struct list_head *add_node(struct list_head * listhead ,char * newcs, int d); struct list_head * print_list_head(struct list_head *list); struct list_head* traverse_dir(char *dirnameA, struct list_head* list1); // 遍歷文件夾尋找目標 struct list_head* find_dir(char *dirnameA); //進入目標路徑typedef int ElemType; //32位下這個結構體是8個字節 //64位下這個結構體是16個字節 struct Node {ElemType data;//"數據域":保存數據元素char cs[500];struct Node *next;//"指針域":保存下一個數據元素的地址struct Node *pre; //實際上就是用來保存數據與數據之間的關系的 }; struct list_head {struct Node *first; //指向鏈表中的第一個節點(首節點)struct Node *last; //指向鏈表中的最后一個節點(尾結點)int num; //節點的個數//char list_name[512]; //鏈表的名字//.. };#endif鏈表c程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "bothlist.h"//頭結點//創建一個帶頭結點的單鏈表 struct list_head *create_list_head(void) {//開辟一塊新的內存空間給頭結點并且對頭結點的成員變量進行初始化struct list_head *listhead = malloc(sizeof(struct list_head));listhead->first = listhead->last = NULL;listhead->num = 0;return listhead; } //將一條帶頭結點的單鏈表進行打印struct list_head *add_node(struct list_head * listhead ,char * newcs, int d) {//開辟新的節點保存獲取到的數據struct Node *pnew = malloc(sizeof(struct Node));pnew->data = d;strcpy( pnew->cs ,newcs);pnew->next = NULL;pnew->pre = NULL;//將新節點尾插到新的帶頭結點的鏈表中去if(listhead->first == NULL){listhead->first = listhead->last = pnew;listhead->first->pre = NULL;listhead->last->pre = NULL; }else//尾插法{listhead->last->next = pnew;pnew->pre = listhead->last;listhead->last = pnew;listhead->last->next = listhead->first;//修改時間9 23 21:10listhead->first->pre = listhead->last;}//鏈表中節點的個數加1listhead->num++;//返回頭節點的地址return listhead; } //將一條帶頭結點的單鏈表進行打印struct list_head * print_list_head(struct list_head *list) {struct Node*p = list->first;int times = list->num; while(times--){printf("%s \n",p->cs);p = p->next;}putchar('\n');printf("此鏈表中節點的個數為%d\n",list->num); }//釋放內存 void free_list_head(struct list_head *list) {struct Node *p = list->first;while(list->first){p = list->first;list->first = list->first->next;p->next = NULL;//釋放摘下來的節點free(p);}//釋放頭節點free(list); } //輸出倒數第k個節點的地址 struct list_head* sentry(struct list_head *list ,int k) {if (k>list->num){printf("???手欠???\n"); }else{struct Node* p1 = list->first;struct Node* p2 = list->first;while(k--){p1 = p1->next;}while(p1){p1 = p1->next;p2 = p2->next;}printf("%c的地址為%p\n",p2->data,&(p2->data));} } void print_list_last(struct list_head *list) {struct Node*p = list->last;while(p){printf("%s \n",p->cs);p = p->pre;}putchar('\n');printf("此鏈表中節點的個數為%d\n",list->num); }功能函數find_target.c
#include <stdio.h> //尋找目標文件.c .cpp #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <dirent.h> #include <string.h> #include <stdlib.h> #include "bothlist.h" /*func:遍歷目錄A的子文件返回值:0成功 -1失敗 */ struct list_head* traverse_dir(char *dirnameA, struct list_head* list1) { // printf("a = %s line = %d func = %s\n",dirnameA,__LINE__,__FUNCTION__);//1、打開目錄DIR *d = opendir(dirnameA);if(d == NULL){perror("open dirname A error");// printf("dirname = %s\n",dirnameA);return NULL;}int qu = 0; //區別jgp和bmpint need = 0; //使用該數字去截取后綴//2、讀取該目錄下所有的子文件/子目錄while(1){struct dirent * p = readdir(d);//讀取目錄項if(p == NULL)//如果讀完了{break;}//3、判斷目錄項中文件名是否是. ..,如果是,就跳過if(strcmp(".",p->d_name) == 0 || strcmp("..",p->d_name) == 0){continue;}//4、如果不是,判斷是目錄還是文件struct stat statbuf;//因為進程的工作路徑問題,只能直接訪問當前目錄下的文件char cbuf[2560] = {0};strcpy(cbuf,dirnameA);//dirnameA -> "/home/china"cbuf[strlen(cbuf)] = '/'; //buf -> "/home/china" => "/home/china/"strncat(cbuf,p->d_name,strlen(p->d_name)); //p->d_name-> "1.txt" => "/home/china/1.txtstat(cbuf, &statbuf);//這個文件在哪一個路徑下面呢? dirnameA => dirnameA/p->d_name// printf("%s",check);need = strlen(cbuf)-2; //截取后綴.xxx if(strcmp(".c",cbuf+need) == 0){qu = 2;list1 = add_node(list1,cbuf,qu);}if(strcmp(".cpp",cbuf+need-2) == 0){qu = 1;list1 =add_node(list1,cbuf,qu);} if(S_ISDIR(statbuf.st_mode)) //如果是目錄再找{traverse_dir(cbuf,list1); }}closedir(d); //關閉目錄 return list1; }/*func:尋找MP3返回值:0成功 -1失敗 */struct list_head* find_dir(char *dirnameA) {if(dirnameA == NULL)//判斷輸入是不是有問題{return NULL;}struct list_head * list1;list1 = create_list_head();list1 = traverse_dir(dirnameA,list1); //print_list_head(list1); return list1; } int main(int argc, char* argv[]) {if (argc < 2){printf("./a.out road\n");exit(EXIT_FAILURE);}struct list_head* list1 = find_dir(argv[1]);print_list_head(list1); return 0; }太晚了,有時間再簡化 這是直接從一個項目中搬出來的.
總結
以上是生活随笔為你收集整理的该文章为递归寻找目录下目标文件(待完善,但是能用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现tree系统命令
- 下一篇: 几个简单的排序方式1