C++遍歷文件夾下文件的辦法。本站提示廣大學習愛好者:(C++遍歷文件夾下文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++遍歷文件夾下文件的辦法正文
本文實例講述了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, "\\*.*");
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++法式設計有所贊助。