成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

讀取指定文件夾內所有文件列表

開發 前端
今天分析下利用 scandir 函數獲取文件列表。

今天分析下利用 scandir 函數獲取文件列表。

函數原型

#include <dirent.h>

int scandir(const char *restrict dirp,
struct dirent ***restrict namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **,const struct dirent **));

scandir() 會掃描目錄 dirp(不包括子目錄),經由參數 filter 指定的函數來挑選符合條件的目錄結構至參數namelist 數組中,最后再調用參數 compar 指定的函數來排序 namelist 數組中的目錄數據。

每次從 dirp 中讀取的目錄結構后都會傳遞給 filter 進行過濾,若 filter 返回 0 則不會把該目錄結構復制到 namelist 數組中。

若 filter 參數為 NULL,則選擇所有目錄到 namelist 組中。

scandir() 中會調用 qsort() 來對獲取的目錄列表進行排序,參數 compar 則為 qsort() 的參數,若是要把目錄名稱列表按照字母順序排序則 compar 參數可使用 alphasort()。

返回值 : 返回獲取到的目錄項的數量。如果發生錯誤,則返回-1,并設置errno 以指示錯誤。

例子

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

int main(void)
{
struct dirent **namelist;
int n;

n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}

while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}

free(namelist);

exit(EXIT_SUCCESS);
}

運行結果

#./test
tdir
libc.c
libb.c
liba.c
gg.h
..
.

該結果是按照下標倒序顯示的,也可以從下標 0 開始顯示,這樣就是按照字母排序的了。

使用 filter 參數進行過濾

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

int myfilter(const struct dirent *entry)
{
return strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..");
}



int main(void)
{
struct dirent **namelist;
int n;

n = scandir(".", &namelist, myfilter, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}

while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}

free(namelist);

exit(EXIT_SUCCESS);
}

運行結果

#./test
tdir
libc.c
libb.c
liba.c
gg.h

獲取以 lib 開頭的文件

int myfilter(const struct dirent *ent)
{
if(ent->d_type != DT_REG)
return 0;

return (strncmp(ent->d_name, "lib", 3) == 0);
}

運行結果如下:

#./test
libc.c
libb.c
liba.c
責任編輯:華軒 來源: 今日頭條
相關推薦

2020-09-23 08:53:48

父文件夾模塊Python

2021-11-17 09:01:23

Python批量合并Python基礎

2009-08-12 16:57:28

C#讀取文件夾

2021-11-19 08:59:28

Python 批量合并

2017-04-07 11:00:25

Windows 7Windows自動備份

2009-10-27 08:56:22

VB.NET文件夾

2010-03-25 10:24:32

Python代碼

2010-12-31 13:35:25

文件夾重定向

2009-12-03 10:18:32

Linux文件夾執行權限

2024-12-06 15:11:34

Python文件夾目錄

2020-05-09 16:25:31

Ubuntu文件夾桌面

2015-03-13 13:50:47

Java讀取文件夾大小Java讀取文件Java讀取

2011-08-04 15:36:32

文件夾病毒

2011-03-04 16:37:13

FileZilla

2010-03-05 16:54:47

2009-12-09 10:10:08

PHP創建文件夾

2010-07-14 21:10:09

VirtualBox

2021-04-01 16:36:07

macOS文件夾磁盤

2023-05-13 17:43:17

Linux文件文件夾

2021-08-16 13:34:07

Linux終端刪除文件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 男女国产网站 | 日韩欧美在线免费观看 | 一区二区国产精品 | 免费观看视频www | 日韩精品一区二区三区中文在线 | 中文字幕在线观看一区 | 激情六月丁香婷婷 | 国产精品高潮呻吟久久av黑人 | 一区二区三区四区在线免费观看 | 成年女人免费v片 | 欧美在线a | 91精品无人区卡一卡二卡三 | 四虎影院免费在线播放 | 97精品视频在线 | 中文字幕久久久 | 欧美日韩久久 | 久久一区二区三区免费 | 久久国产成人午夜av影院武则天 | 成人毛片一区二区三区 | jizz中国日本 | 狠狠操狠狠干 | 亚洲视频在线一区 | 国产免费拔擦拔擦8x高清 | 亚洲视频网 | 欧美群妇大交群中文字幕 | 久久精品国产精品青草 | 成人一区二区三区在线观看 | 日本福利在线观看 | 狠狠干美女 | 超碰97免费在线 | 日韩视频精品在线 | 欧美综合一区二区三区 | 中文字幕亚洲精品 | 成人av一区二区三区 | 国产精品视频999 | 精品国产高清一区二区三区 | 日韩中文字幕区 | www.99精品 | 久久精品无码一区二区三区 | 天天亚洲 | www.xxxx欧美|