當前位置:首頁 » 知網查重 » c遍歷目錄

c遍歷目錄

發布時間: 2021-03-14 13:44:20

❶ C語言:如何遍歷指定的文件夾(可以包括子文件夾)中的每一個文件名

Function SearchFiles(Path As String, FileType As String)
Dim Files() As String '文件路徑
Dim Folder() As String '文件夾路徑
Dim a, b, c As Long
Dim sPath As String

sPath = Dir(Path & FileType) '查找第一個文件

Do While Len(sPath) '循環到沒有文件為止
a = a + 1
ReDim Preserve Files(1 To a)
Files(a) = Path & sPath '將文件目錄和文件名組合,並存放到數組中
List1.AddItem Files(a) '加入list控制項中
sPath = Dir '查找下一個文件
DoEvents '讓出控制權
Loop

sPath = Dir(Path & "\", vbDirectory) '查找第一個文件夾

Do While Len(sPath) '循環到沒有文件夾為止
If Left(sPath, 1) <> "." Then '為了防止重復查找
If GetAttr(Path & "\" & sPath) And vbDirectory Then '如果是文件夾則。。。。。。
b = b + 1
ReDim Preserve Folder(1 To b)
Folder(b) = Path & sPath & "\" '將目錄和文件夾名稱組合形成新的目錄,並存放到數組中
End If
End If
sPath = Dir '查找下一個文件夾
DoEvents '讓出控制權
Loop

For c = 1 To b '使用遞歸方法,遍歷所有目錄
SearchFiles Folder(c), FileType
Next

End Function

Private Sub Command1_Click() '調用
SearchFiles "e:\", "*.exe"
End Sub

❷ C語言遍歷文件目錄

//沒調試,簡單寫了下

int text()
{
DIR *pd = NULL;
struct dirent *ptr = NULL;

//打開目錄,pd為該目錄句柄
if ( (pd = opendir("d:/")) == NULL)
{
puts("open failed");
return -1;
}

//遍歷目錄
while ( (ptr = readdir(pd)) != NULL)
{
//輸出目錄下文件名
puts(ptr->d_name);
}

return 0;
}

❸ windows c 怎麼遍歷目錄文件

1、操作系統中有相關的API函數,可以讀取目錄中所有的文件名字,以及時間屬性信息,把這些信息讀出來,直接依次遍歷即可。
2、常式:
#include"stdio.h"
#include"io.h"
int main()
{
struct _finddata_t files;
int File_Handle;
int i=0;
File_Handle = _findfirst("c:/temp/*.txt",&files);
if(File_Handle==-1)
{
printf("error\n");
return 0;
}
do
{
printf("%s \n",files.name);
i++;
}while(0==_findnext(File_Handle,&files));
_findclose(File_Handle);
printf("Find %d files\n",i);
return 0;
}

❹ C語言遍歷目錄

vc6編譯運行通過內容
#include"stdio.h"
#include"io.h"
int main()
{
struct _finddata_t files;
int File_Handle;
int i=0;

File_Handle = _findfirst("c:/temp/*.txt",&files);
if(File_Handle==-1)
{
printf("error\n");
return 0;
}
do
{
printf("%s \n",files.name);
i++;
}while(0==_findnext(File_Handle,&files));
_findclose(File_Handle);
printf("Find %d files\n",i);
return 0;
}

❺ 如何用C實現遍歷文件夾

在Win32平台下不用windows api,有好多功能實現起來都很費勁,特別是系統相關的
再說用api又不是什麼丟人的事。開發可移植程序除外。
用FindFirstFile和FindNextFile兩個api很容易實現
////////////////////////////////////////////////
void FindFile(LPCTSTR lpszPath) {
TCHAR szFind[MAX_PATH];
lstrcpy(szFind,lpszPath);
if(!IsRoot(szFind)) lstrcat(szFind,"\\");
lstrcat(szFind,"*.*");
WIN32_FIND_DATA wfd;
HANDLE hFind=FindFirstFile(szFind,&wfd);
if(hFind==INVALID_HANDLE_VALUE) return;
do{
if(lstrcmp(wfd.cFileName,".")==0||lstrcmp(wfd.cFileName,"..")==0) continue;
char szFile[MAX_PATH];
lstrcpy(szFile,lpszPath);
if(!IsRoot(szFile)) lstrcat(szFile,"\\");
lstrcat(szFile,wfd.cFileName);
if((GetFileAttributes(szFile)&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY){
FindFile(szFile); //遞歸
}
else {
} //Do your things
}
} while (FindNextFile(hFind,&wfd));
CloseHandle(hFind);
}

❻ C/C++中的readdir遍歷目錄中的子目錄的問題

這個要正對不同的操作系統,不同的操作系統,文件節點的存放方式不一樣,讀取的內方式就不一容樣了。
可以利用for循環,像Linux dir=opendir (dirname)打開文件夾,返回目錄指針,dp=readdir(dir)利用讀目錄,返回一行行讀取目錄的文件結構體指針,指針中存的有文件屬性,是文件,還是文件夾。
通過判斷是文件或者文件夾:
如果是文件,就就輸出文件名dp->name
否則,就是一個文件夾 繼續dir1=opendir(dp->name),dp1=readdir(dir1)..
一直循環到判斷不到目錄了。
windows應該也有類似的函數吧,這個我就么有用過了,你找找。

❼ C語言遍歷目錄樹下所有文件

完全可以啊 vs2012 CFile類可以搞定

❽ C語言如何實現遍歷目錄的功能

不同系統 使用的介面函數可能不同
Linux要用Linux介面
windows要用win api

基本思路就是用opendir打開目錄
然後循環版readdir 直到null
如果要遞歸,那麼對權於每個read到的文件夾 都要再調用一次遍歷函數。

❾ windows下使用C/C++怎麼遍歷目錄並讀取目錄下的文件列表

用C的函數實現,windows ,linux 都能用

#include<iostream>
#include<io.h>
#include<string>
usingnamespacestd;
voiddir(stringpath)
{
longhFile=0;
struct_finddata_tfileInfo;
stringpathName,exdName;
//\*代表要遍歷所有的類型
if((hFile=_findfirst(pathName.assign(path).append("\*").c_str(),&fileInfo))==-1){
return;
}
do
{
//判斷文件的屬性是文件夾還是文件
cout<<fileInfo.name<<(fileInfo.attrib&_A_SUBDIR?"[folder]":"[file]")<<endl;
}while(_findnext(hFile,&fileInfo)==0);
_findclose(hFile);
return;
}
intmain()
{
//要遍歷的目錄
stringpath="E:\work\\test4";
dir(path);
system("pause");
return0;
}

❿ C語言如何遍歷目錄 (C++也可以) findfirst findnext怎麼用

WIN32_FIND_DATAfd;
HANDLEhdFind;
hdFind=FindFirstFile(strDirPath,&fd);//strDirPath為目錄路徑
if(hdFind!=INVALID_HANDLE_VALUE)
{
do
{
//做你需要的操作。
}while(FindNextFile(hdFind,&fd));
}

熱點內容
塗鴉論文 發布:2021-03-31 13:04:48 瀏覽:698
手機資料庫應用 發布:2021-03-31 13:04:28 瀏覽:353
版面217 發布:2021-03-31 13:04:18 瀏覽:587
知網不查的資源 發布:2021-03-31 13:03:43 瀏覽:713
基金贖回參考 發布:2021-03-31 13:02:08 瀏覽:489
懸疑故事範文 發布:2021-03-31 13:02:07 瀏覽:87
做簡單的自我介紹範文 發布:2021-03-31 13:01:48 瀏覽:537
戰略地圖參考 發布:2021-03-31 13:01:09 瀏覽:463
收支模板 發布:2021-03-31 13:00:43 瀏覽:17
電氣學術會議 發布:2021-03-31 13:00:32 瀏覽:731