當(dāng)前位置:
首頁(yè) >
该文章为递归寻找目录下目标文件(待完善,但是能用)
發(fā)布時(shí)間:2023/12/15
32
豆豆
生活随笔
收集整理的這篇文章主要介紹了
该文章为递归寻找目录下目标文件(待完善,但是能用)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先是鏈表,目前設(shè)置為雙向鏈表還未簡(jiǎn)化.
頭文件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); // 遍歷文件夾尋找目標(biāo) struct list_head* find_dir(char *dirnameA); //進(jìn)入目標(biāo)路徑typedef int ElemType; //32位下這個(gè)結(jié)構(gòu)體是8個(gè)字節(jié) //64位下這個(gè)結(jié)構(gòu)體是16個(gè)字節(jié) struct Node {ElemType data;//"數(shù)據(jù)域":保存數(shù)據(jù)元素char cs[500];struct Node *next;//"指針域":保存下一個(gè)數(shù)據(jù)元素的地址struct Node *pre; //實(shí)際上就是用來(lái)保存數(shù)據(jù)與數(shù)據(jù)之間的關(guān)系的 }; struct list_head {struct Node *first; //指向鏈表中的第一個(gè)節(jié)點(diǎn)(首節(jié)點(diǎn))struct Node *last; //指向鏈表中的最后一個(gè)節(jié)點(diǎn)(尾結(jié)點(diǎn))int num; //節(jié)點(diǎn)的個(gè)數(shù)//char list_name[512]; //鏈表的名字//.. };#endif鏈表c程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "bothlist.h"//頭結(jié)點(diǎn)//創(chuàng)建一個(gè)帶頭結(jié)點(diǎn)的單鏈表 struct list_head *create_list_head(void) {//開辟一塊新的內(nèi)存空間給頭結(jié)點(diǎn)并且對(duì)頭結(jié)點(diǎn)的成員變量進(jìn)行初始化struct list_head *listhead = malloc(sizeof(struct list_head));listhead->first = listhead->last = NULL;listhead->num = 0;return listhead; } //將一條帶頭結(jié)點(diǎn)的單鏈表進(jìn)行打印struct list_head *add_node(struct list_head * listhead ,char * newcs, int d) {//開辟新的節(jié)點(diǎn)保存獲取到的數(shù)據(jù)struct Node *pnew = malloc(sizeof(struct Node));pnew->data = d;strcpy( pnew->cs ,newcs);pnew->next = NULL;pnew->pre = NULL;//將新節(jié)點(diǎn)尾插到新的帶頭結(jié)點(diǎn)的鏈表中去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;//修改時(shí)間9 23 21:10listhead->first->pre = listhead->last;}//鏈表中節(jié)點(diǎn)的個(gè)數(shù)加1listhead->num++;//返回頭節(jié)點(diǎn)的地址return listhead; } //將一條帶頭結(jié)點(diǎn)的單鏈表進(jìn)行打印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("此鏈表中節(jié)點(diǎn)的個(gè)數(shù)為%d\n",list->num); }//釋放內(nèi)存 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;//釋放摘下來(lái)的節(jié)點(diǎn)free(p);}//釋放頭節(jié)點(diǎn)free(list); } //輸出倒數(shù)第k個(gè)節(jié)點(diǎn)的地址 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("此鏈表中節(jié)點(diǎn)的個(gè)數(shù)為%d\n",list->num); }功能函數(shù)find_target.c
#include <stdio.h> //尋找目標(biāo)文件.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; //區(qū)別jgp和bmpint need = 0; //使用該數(shù)字去截取后綴//2、讀取該目錄下所有的子文件/子目錄while(1){struct dirent * p = readdir(d);//讀取目錄項(xiàng)if(p == NULL)//如果讀完了{(lán)break;}//3、判斷目錄項(xiàng)中文件名是否是. ..,如果是,就跳過if(strcmp(".",p->d_name) == 0 || strcmp("..",p->d_name) == 0){continue;}//4、如果不是,判斷是目錄還是文件struct stat statbuf;//因?yàn)檫M(jìn)程的工作路徑問題,只能直接訪問當(dāng)前目錄下的文件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);//這個(gè)文件在哪一個(gè)路徑下面呢? 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); //關(guān)閉目錄 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; }太晚了,有時(shí)間再簡(jiǎn)化 這是直接從一個(gè)項(xiàng)目中搬出來(lái)的.
總結(jié)
以上是生活随笔為你收集整理的该文章为递归寻找目录下目标文件(待完善,但是能用)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现tree系统命令
- 下一篇: 几个简单的排序方式1