文件遍历
调用系统API
#include <windows.h> #include <stdio.h> #include <string.h> #define LEN 1024 void DirectoryList(LPCSTR Path) { WIN32_FIND_DATA FindData; HANDLE hError; int FileCount = 0; char FilePathName[LEN]; char FullPathName[LEN]; strcpy(FilePathName, Path); strcat(FilePathName, "\\*.*"); hError = FindFirstFile(FilePathName, &FindData); if (hError == INVALID_HANDLE_VALUE) { printf("搜索失败!"); return; } while(::FindNextFile(hError, &FindData)) { if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0 ) { continue; } wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName); FileCount++; printf("\n%d %s ", FileCount, FullPathName); if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { printf("<Dir>"); DirectoryList(FullPathName); } } } int main() { char path[LEN]; printf("please input the file Directory\n"); scanf("%s",path); DirectoryList(path); }
Keep it simple!