導航:首頁 > 文件目錄 > c獲取路徑下所有文件

c獲取路徑下所有文件

發布時間:2024-11-27 19:29:25

Ⅰ 如何獲取目錄下所有文件名 c++

以下程序只能在Windows下運行,完全按照c語言編寫。但是添加了windows.h庫。以下程序在Visual C++ 2008下編譯通過。

#undef UNICODE // 如果你不知道什麼意思,請不要修改
#define MAX_RESULT 256

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

char** EnumFiles(const char *directory, int *count)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char result[MAX_RESULT][MAX_PATH];
char **returnresult;
char pattern[MAX_PATH];
int i = 0, j;

// 開始查找
strcpy(pattern, directory);
strcat(pattern, "\\*.*");
hFind = FindFirstFile(pattern, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
*count = 0;
return NULL;
}
else
{
do
{
strcpy(result[i++], FindFileData.cFileName);
}
while (FindNextFile(hFind, &FindFileData) != 0);
}

// 查找結束
FindClose(hFind);

// 復制到結果中
returnresult = (char **)calloc(i, sizeof(char *));

for (j = 0; j < i; j++)
{
returnresult[j] = (char *)calloc(MAX_PATH, sizeof(char));
strcpy(returnresult[j], result[j]);
}

*count = i;
return returnresult;
}

void main()
{
int i, count;
char ** result;
char directory[MAX_PATH];

printf("請輸入要查詢的文件夾:");
scanf("%s", directory);

result = EnumFiles(directory, &count);

for (i = 0; i < count; i++)
printf("%s\n", result[i]);

}

Ⅱ C語言怎麼讀取某一文件夾下的所有文件夾和文件

讀取的代碼方式如下:

intmain()

{

longfile;

struct_finddata_tfind;

_chdir("d:\");

if((file=_findfirst("*.*",&find))==-1L)

{

printf("空白! ");

exit(0);

}

printf("%s ",find.name);

while(_findnext(file,&find)==0)

{

printf("%s ",find.name);

}

_findclose(file);

return0;

}

Ⅲ VC環境中用C語言查找當前路徑下的所有文件和文件夾的函數是什麼

這是我的TFTP程序中的一個函數,是搜索當前盤符下的所有文件,包括文件的大小,並發送到客戶端,其中就有查找當前路徑下的文件,你自己挑一下,應該能完成你的需求。
void FileList(sockaddr_in sour_addr,char strStartDir[])
{
char sendbuffer[1024];
sockaddr_in destaddr;

int sourlen = 0;
int ret = 0;
int len = 0;
int flen = 0;

fd_set fdr;

unsigned short blocknum = 0;

FILE *file;
char filename[128];

strcpy(filename,strStartDir+2); /*獲取文件名*/

strcat(filename,"\\*");
destaddr.sin_family = AF_INET;
destaddr.sin_port = sour_addr.sin_port;
destaddr.sin_addr.s_addr = inet_addr(desthost);//

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(filename, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle");
}
else
{
while(FindNextFile(hFind,&FindFileData))
{
printf(FindFileData.cFileName);
printf("\r\n");
memset(sendbuffer,'\0',1024);

len = filldata(blocknum++,FindFileData.cFileName,strlen(FindFileData.cFileName),sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)&destaddr,sizeof(destaddr));

}
len = fillover(blocknum,"Over",4,sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)&destaddr,sizeof(destaddr));
FindClose(hFind);
return;
}
}

Ⅳ 急~!!!如何用C/C++ 讀取文件夾中所有文件(如.csv文件)

CString pathWild = "你的路徑" + _T("\\*.csv");
struct _finddata_t c_file;
long hFile;
if( (hFile = _findfirst( LPCTSTR(pathWild), &c_file )) == -1L)
{
MessageBox("選擇目錄下並無csv文件,請確認");
_findclose(hFile);
return;
}
else
{
do
{
//這里就是文件名,加專上之前的路徑就是完整路徑了屬
CString strFileName = c_file.name;
}
while (_findnext(hFile, &c_file) == 0);
}
_findclose(hFile);

Ⅳ C語言:如何得到指定地址的文件夾中所有文件的文件名和其修改時間 包括子文件內的

//獲取指定目錄下的所有文件列表 author:wangchangshaui jlu
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;

//打開目錄
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//讀取目錄
while ((ent = readdir(pDir)) != NULL)
{ //統計當前文件夾下有多少文件(不包括文件夾)
//得到讀取文件的絕對路徑名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判斷是目錄還是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//關閉目錄
closedir(pDir);
// myLog("共%d個文件\n", count);

//開辟字元指針數組,用於下一步的開辟容納文件名字元串的空間
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}

//打開目錄
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//讀取目錄
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
//得到讀取文件的絕對路徑名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判斷是目錄還是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d個文件:%s\n", i, ent->d_name);
i++;
}
} //for
//關閉目錄
closedir(pDir);

*fileCount = count;
return fileNameList;
}

Ⅵ 如何用c語言獲得一個目錄下所有文件的文件名

void enum_path(char *cpath){
WIN32_FIND_DATA wfd;
HANDLE hfd;
char cdir[MAX_PATH];
char subdir[MAX_PATH];
int r;
GetCurrentDirectory(MAX_PATH,cdir);
SetCurrentDirectory(cpath);
hfd = FindFirstFile("*.*",&wfd);
if(hfd!=INVALID_HANDLE_VALUE) {
do{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(wfd.cFileName[0] != '.') {
// 合成完整路徑名
sprintf(subdir,"%s\\%s",cpath,wfd.cFileName);
// 遞歸枚舉子目錄
enum_path(subdir);
}
}else{
printf("%s\\%s\n",cpath,wfd.cFileName);
// 病毒可根據後綴名判斷是
// 否要感染相應的文件
}
}while(r=FindNextFile(hfd,&wfd),r!=0);
}
SetCurrentDirectory(cdir);
}

閱讀全文

與c獲取路徑下所有文件相關的資料

熱點內容
達內學安卓怎麼樣 瀏覽:300
word2010並排兩個表格 瀏覽:284
酷派安卓如何升級版本 瀏覽:684
linuxidl文件 瀏覽:730
校園網站問題及如何解決網站問題 瀏覽:256
華為鴻蒙錄音文件在哪個文件夾 瀏覽:900
psraw預設在哪個文件 瀏覽:385
文件名後面的zip是什麼意思 瀏覽:722
jsutf8gbk 瀏覽:261
蘋果5怎樣換屏幕圖標 瀏覽:452
微信上曬照片有危險嗎 瀏覽:499
繞過改密碼登錄密碼登錄密碼登錄密碼登錄 瀏覽:450
風暴英雄當前版本最強 瀏覽:104
餘姚數控編程培訓哪裡專業 瀏覽:419
qq空間66版本下載 瀏覽:908
有一款看美劇的app是什麼 瀏覽:397
前端後端json資料庫 瀏覽:267
vi文件格式linux 瀏覽:963
php如何引用js文件 瀏覽:531
word轉成pdf怎麼設置漸變色背景 瀏覽:655

友情鏈接