C++遍歷文件夾下文件的方法
更新時間:2015年07月16日 10:16:21 作者:宋勇野
這篇文章主要介紹了C++遍歷文件夾下文件的方法,實例分析了C++針對文件夾遍歷的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C++遍歷文件夾下文件的方法。分享給大家供大家參考。具體如下:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define LEN 1024
// 深度優(yōu)先遞歸遍歷目錄中所有的文件
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++程序設計有所幫助。
相關文章
Qt使用QCamera實現(xiàn)切換相機,分辨率和圖像捕獲功能
這篇文章主要為大家介紹了如何利用Qt中的相機類QCamera,取景器類QCameraViewfinder,圖像捕獲類QCameraImageCapture實現(xiàn)切換相機、分辨率和圖像捕獲功能,需要的可以了解一下2023-04-04
C++中CString string char* char 之間的字符轉換(多種方法)
在寫程序的時候,我們經常遇到各種各樣的類型轉換,比如 char* CString string 之間的互相轉換,這里簡單為大家介紹一下,需要的朋友可以參考下2017-09-09

