導航:首頁 > 文件類型 > c目錄所有文件名

c目錄所有文件名

發布時間:2025-08-25 05:27:13

⑴ 怎樣使用C語言列出某個目錄下的文件

C語言本身沒有提供象dir_list()這樣的函數來列出某個目錄下所有的文件。不過,利用C語言的幾個目錄函數,你可以自己編寫一個dir_list()函數。 首先,頭文件dos.h定義了一個find_t結構,它可以描述DOS下的文件信息,包括文件名、時間、日期、大小和屬性。其次,C編譯程序庫中有_dos_findfirst()和_dos_findnext()這樣兩個函數,利用它們可以找到某個目錄下符合查找要求的第一個或下一個文件。 dos_findfirst()函數有三個參數,第一個參數指明要查找的文件名,例如你可以用「*.*」指明要查找某個目錄下的所有文件。第二個參數指明要查找的文件屬性,例如你可以指明只查找隱含文件或子目錄。第三個參數是指向一個find_t變數的指針,查找到的文件的有關信息將存放到該變數中。 dos_findnext()函數在相應的目錄中繼續查找由_dos_findfirst()函數的第一個參數指明的文件。_dos_findnext()函數只有一個參數,它同樣是指向一個find_t變數的指針,查找到剛文件的有關信息同樣將存放到該變數中。 利用上述兩個函數和find_t結構,你就可以遍歷磁碟上的某個目錄,並列出該目錄下所有的文件,請看下例: #include <stdio.h> #include <direct.h> #include <dos.h> #include <malloc.h> #include <memory.h> #include <string.h> typedef struct find_t FILE_BLOCK void main(void); void main(void){FILE_BLOCK f-block; /* Define the find_t structure variable * / int ret_code; / * Define a variable to store the return codes * / / * Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. * / ret_code = _dos_findfirst(" *. * ", 0xFF, &f_block); /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. * / while (ret_code == 0){/* Print the file's name * / printf(" %-12s\n, f_block, name); / * Use the -dos_findnext() function to look

⑵ 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;

}

⑶ c++獲取指定目錄下的文件名

可以使用searchenv函數來實現,參考代碼如下:

intmain(void)
{
charpathbuffer[_MAX_PATH];
charsearchfile[]="×.EXE";
charenvvar[]="PATH";

//:
_searchenv(searchfile,envvar,pathbuffer);//C4996
//Note:_searchenvisdeprecated;considerusing_searchenv_s
if(*pathbuffer!='')
printf("Pathfor%s: %s ",searchfile,pathbuffer);
else
printf("%snotfound ",searchfile);
}

⑷ 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目錄所有文件名相關的資料

熱點內容
java屏蔽 瀏覽:243
java怎麼算時間間隔年數 瀏覽:298
怎麼不需要密碼盜qq 瀏覽:261
如何發放網站標識碼 瀏覽:860
百網中文網哪個網站好 瀏覽:55
win7那個文件影響進入系統 瀏覽:766
微雲手機下載的文件路徑 瀏覽:134
qt項目中文件路徑 瀏覽:676
學單片機編程用什麼書 瀏覽:420
vb中剪切代碼 瀏覽:695
em如何創建資料庫 瀏覽:144
編寫一個c程序加減乘除 瀏覽:125
java如何從鍵盤輸入數組 瀏覽:542
蘋果固定撥號如何關閉 瀏覽:871
優盤中病毒怎麼把文件拿出來 瀏覽:502
word圖片怎麼一次存到一個文件夾 瀏覽:349
文件夾內內容排序遞增如何去掉 瀏覽:771
華南師范數據結構掛科率是多少 瀏覽:872
安卓flash插件打不開 瀏覽:208
qq群文件顯示安全風險 瀏覽:376

友情鏈接