本文實例講述了C++遍歷文件夾下文件的方法。分享給大家供大家參考。具體如下:
#include <windows.h>#include <stdio.h>#include <string.h>#define LEN 1024// 深度優先遞歸遍歷目錄中所有的文件BOOL DirectoryList(LPCSTR Path){ WIN32_FIND_DATA FindData; HANDLE hError; int FileCount = 0; char FilePathName[LEN]; // 構造路徑 char FullPathName[LEN]; strcpy(FilePathName, Path); strcat(FilePathName, "http://*.*"); hError = FindFirstFile(FilePathName, &FindData); if (hError == INVALID_HANDLE_VALUE) { printf("搜索失敗!"); return 0; } 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); } } return 0;}void main(){ DirectoryList("D:eclipse-J2EE");}
希望本文所述對大家的C++程序設計有所幫助。
新聞熱點
疑難解答
圖片精選