使用wxWidgets版本
#include <wx/wx.h> #include "wx/dir.h" typedef struct TargetFile TargetFile; struct TargetFile { char *path; TargetFile *next; }; TargetFile* GetTargetFilePath(wxString target_path, wxString filespec) { wxDir dir(target_path); if (!dir.IsOpened()) { return NULL; } TargetFile *head, *temp, *p; head = (TargetFile*) malloc(sizeof(TargetFile)); head->next = NULL; temp = head; wxString filename; bool cont = dir.GetFirst(&filename, filespec, wxDIR_FILES); while (cont) { p = (TargetFile*) malloc(sizeof(TargetFile)); memset(p, 0, sizeof(TargetFile)); wxString filepath = target_path + wxFILE_SEP_PATH + filename; p->path = strdup(filepath.c_str()); temp->next = p; temp = p; cont = dir.GetNext(&filename); } p->next = NULL; return head; } int main() { TargetFile *head = GetTargetFilePath(wxT("E:\\software\\TestData\\pdf"), wxT("*.pdf")); TargetFile *print = head->next; while (print) { std::cout << print->path << std::endl; print = print->next; } free(head); free(print); return 0; }
VC版本
#include "stdafx.h" #include <windows.h> #include <stdio.h> #include <string.h> #define Length 1024
TargetFile * DirectoryList(char *path, char *extension) { WIN32_FIND_DATA FindData; char targetpath[Length]; char file_path[Length]; strcpy(targetpath, path); strcat(targetpath, "\\*.pdf"); HANDLE hFind = FindFirstFile(targetpath, &FindData); if (hFind == INVALID_HANDLE_VALUE) { return NULL; } TargetFile *head = NULL; TargetFile *p, *temp; head = (TargetFile *) malloc(sizeof(TargetFile )); head->next = NULL; temp = head; while (true) { wsprintf(file_path, "%s\\%s", path, FindData.cFileName); p = (TargetFile *) malloc(sizeof(TargetFile )); p->name = strdup(file_path); temp->next = p; temp = p; if (!FindNextFile(hFind, &FindData)) { break; } } p->next = NULL; return head; }