Skip to content

Instantly share code, notes, and snippets.

@hunandy14
Created April 19, 2021 18:28
Show Gist options
  • Save hunandy14/24a5e7690a18e6d3cf320e68d8ccc7fd to your computer and use it in GitHub Desktop.
Save hunandy14/24a5e7690a18e6d3cf320e68d8ccc7fd to your computer and use it in GitHub Desktop.
C 讀取任務目錄檔案名稱
/*****************************************************************
Name : ReadFiles path
Date : 2017/05/22
By : CharlotteHonG
Final: 2021/04/20
作者:https://charlottehong.blogspot.com/2017/07/c-c.html
*****************************************************************/
#include <stdio.h>
#include <string.h>
#include <io.h>
#define __FILENAME__ strrchr("\\" __FILE__, '\\') + 1
#define POINT_IS_NULL(msg) \
printf("%s \t\t # %s::%d --> \"%s()\" \n" , \
(msg) , __FILENAME__, __LINE__, __FUNCTION__)
#ifndef __cplusplus
#define nullptr NULL
#define or ||
#define and &&
#define OR ||
#define AND &&
#endif // !__cplusplus
//====================================================================================
typedef struct ListNode ListNode;
struct ListNode{
char* data;
ListNode* next;
};
void List_basic_ctor(ListNode* _this, const char* s){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
char* buff = nullptr;
if(s){
buff = (char*)malloc(sizeof(char)*strlen(s)+1);
strcpy(buff, s);
}
_this->data = buff;
_this->next = nullptr;
}
void List_basic_dtor(ListNode* _this){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
if(_this->data) free(_this->data);
_this->data = nullptr;
_this->next = nullptr;
}
ListNode* List_basic_new(const char* s){
ListNode* _this = (ListNode*)malloc(sizeof(ListNode));
List_basic_ctor(_this, s);
return _this;
}
void List_basic_delete(ListNode* _this){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
List_basic_dtor(_this);
free(_this);
}
//------------------------------------------------------------------
void List_basic_append(ListNode* _this, const char* s) {
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
int buff_len = strlen(s) + strlen(_this->data);
char* buff = (char*)malloc(sizeof(char)*buff_len + 1);
strcpy(buff, _this->data);
strcat(buff, s);
if(_this->data) free(_this->data);
_this->data = buff;
}
//====================================================================================
typedef struct List List;
struct List{
ListNode* listHead;
ListNode* listEnd;
int ListNum;
};
void List_ctor(List* _this){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
_this->listHead = List_basic_new(nullptr);
_this->listEnd = _this->listHead;
_this->ListNum = 0;
}
void List_dtor(List* _this){
if (!_this) { POINT_IS_NULL("point is NULL"); }
if(_this->listHead){
// 釋放鏈結資源
ListNode* node = nullptr;
for(ListNode* l=_this->listHead->next; l; l=l->next){
if(node)
List_basic_delete(node);
node=l;
} List_basic_delete(node);
List_basic_delete(_this->listHead);
}
// 釋放本地資源
_this->listHead = nullptr;
_this->listEnd = nullptr;
_this->ListNum = 0;
}
List* List_new(){
List* _this = (List*)malloc(sizeof(List));
List_ctor(_this);
return _this;
}
void List_delete(List* _this){
if (!_this) { POINT_IS_NULL("point is NULL"); return;}
List_dtor(_this);
free(_this);
}
//------------------------------------------------------------------
void List_print(List* _this){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
for(ListNode* l=_this->listHead->next; l; l=l->next){
printf("%s\n", l->data);
}
}
void List_append(List* _this, const char* s){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
ListNode* new_node = List_basic_new(s);
_this->listEnd->next = new_node; // 把新點接上
_this->listEnd = new_node; // 更新結尾點
++_this->ListNum; // 累計計數
}
void List_strSlice(List* _this, const char* src, const char* delim){
if (!_this) { POINT_IS_NULL("point is NULL"); return; }
char* buff = (char*)malloc(sizeof(char)*strlen(src)+1);
strcpy(buff, src);
for(char* pch = strtok(buff, delim); pch; pch = strtok(NULL, delim)){
List_append(_this, pch);
}
}
//====================================================================================
// 獲取目錄中的所有特定檔案
void getFileList(List* list, const char* _dirPath, const char* extenName){
struct _finddata_t file;
intptr_t hFile;
// 修正路徑
char dirPath[256] = {0};
if (_dirPath[strlen(_dirPath)-1]!='\\')
sprintf(dirPath, "%s\\", _dirPath);
else
sprintf(dirPath, "%s", _dirPath);
// 檢查路徑是否有效
char buff[256] = {0};
sprintf(buff, "%s%s", dirPath, extenName);
if ((hFile = _findfirst(buff, &file)) == -1)
perror("path error"), exit(1);
int i=0;
// 開始搜索
do {
// 避開當前目錄[.]和上一層目錄[..]
if (!(strcmp(file.name, ".")) || !(strcmp(file.name, "..")))
continue;
// 子目錄
if (file.attrib == _A_SUBDIR) {
sprintf(buff, "%s%s", dirPath, file.name);
getFileList(list, buff, extenName);
// 檔案
} else {
sprintf(buff, "%s%s", dirPath, file.name);
//printf("%s\n", buff);
List_append(list, buff);
}
} while (_findnext(hFile, &file)==0);
}
//====================================================================================
int main(int argc, char const* argv[]) {
List* list = List_new();
const char* dirPath="Z:\\a";
const char* extenName="*.*";
getFileList(list, dirPath, extenName);
List_print(list);
List_delete(list);
return 0;
}
//====================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment