【文章摘要】
本文以實際的C源程式為例子,介紹了Linux下的目錄掃描函數(scandir)的使用方法,為相關開發工作的開展提供了有益的參考。
【關鍵詞】
C語言 Linux 目錄掃描 makefile scandir
scandir函數的聲明為:
int scandir(const char *dir, structdirent ***namelist, int (*filter) (const void *b), int ( * compare )( conststruct dirent **, const struct dirent ** ));
scandir函數掃描dir目錄下(不包括子目錄)的滿足filter過濾模式的檔案,傳回的結果是由compare函數經過排序的,并儲存在namelist中。對于排序函數,alphasort和versionsort使用得比較多。
當函數執行成功時,傳回的是找到的滿足條件的檔案的個數,如果執行失敗則傳回-1。
本文中的程式用于實作掃描檔案目錄的功能。本程式要将滿足“Test_”字首的檔案掃描并列印出來,掃描目錄為“/zhouzx/TestDir”。
程式流程如圖1所示。

圖1 程式流程圖
本文中的源程式命名為“ScanDirOper.c”,放置到“ScanDirOper”工程之下,并用makefile對源程式進行編譯。最終生成的檔案為“TestScanDir”。
1. “ScanDirOper.c”檔案代碼内容
/**********************************************************************
* 版權所有 (C)2014, Zhou Zhaoxiong。
*
* 檔案名稱: ScanDirOper.c
* 檔案辨別:無
* 内容摘要:用于進行目錄掃描的相關操作
* 其它說明:無
* 目前版本:V1.0
* 作 者:周兆熊
* 完成日期:20140804
* 修改記錄1:
* 修改日期: 20140804
* 版 本 号: V1.0
* 修 改 人:周兆熊
* 修改内容:建立
**********************************************************************/
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <ftw.h>
// 資料類型
typedef unsigned char UINT8;
typedef signed int INT32;
typedef unsigned int UINT32;
// 函數聲明
INT32 SelectFlies(const struct dirent *pDir);
INT32 main(void);
* 功能描述:根據字首選擇檔案
* 輸入參數: dir-目錄
* 輸出參數:無
* 返 回 值: 0-失敗
1-成功
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------------------------------------------
* 20140804 V1.0 ZhouZhaoxiong 建立
***********************************************************************/
INT32 SelectFlies(const struct dirent *pDir)
{
INT32 iPrefixLen = 0;
INT32 iLoopFlag = 0;
INT32 iSelectResult = 0;
if (pDir == NULL)
{
printf("SelectFlies():input parameter is NULL!");
return 0;
}
//比對檔案字首
iPrefixLen =strlen("Test_"); // 字首為"Test_"
iSelectResult = (0 ==strncmp(pDir->d_name, "Test_", iPrefixLen));
if (iSelectResult ==1) // 找到了比對字首的檔案
return 1;
else
}
/****************************************************************
* 功能描述: 主函數
* 輸入參數: 無
* 輸出參數: 無
* 返 回 值: 0-執行成功
-1-執行失敗
* 其他說明: 無
* 修改日期 版本号 修改人 修改内容
* --------------------------------------------------------------------------
* 20140804 V1.0 Zhou Zhaoxiong 建立
****************************************************************/
INT32 main(void)
UINT8 szDirectory[256] = {0};
INT32 iRetValue = 0;
struct dirent**namelist = NULL;
UINT32 iLoopFlag = 0;
memcpy(szDirectory,"/zhouzx/TestDir", strlen("/zhouzx/TestDir"));
// 用scandir函數來選擇檔案, 選出來的檔案按照字母表順序排列
iRetValue =scandir(szDirectory, &namelist, SelectFlies, alphasort);
// 傳回負值,是因為沒有掃描目錄,或掃描出錯
if (iRetValue < 0)
printf("Execscandir failed, please check!");
return -1;
printf("The amount of scanded fileis:%d\n", iRetValue);
// 将掃描到的檔案列印出來
for (iLoopFlag = 0;iLoopFlag < iRetValue; iLoopFlag ++)
printf("Thescanded file %d is:%s\n", iLoopFlag +1, namelist[iLoopFlag]->d_name);
return 0;
2. makefile檔案的内容
TestScanDir : ScanDirOper.c
gcc -c -g ScanDirOper.c
gcc -g -o release/TestScanDir ScanDirOper.o
rm *.o
将本工程檔案傳到Linux機器上。
1. 掃描目錄下的檔案放置
檔案放置如圖2所示。
圖2 掃描目錄下的檔案放置
2. makefile檔案運作過程
在makefile檔案所在目錄下(“ScanDirOper”工程之下)輸入“make”指令,執行結果如下:
zhou@linux:~/zhouzx/ScanDirOper> make
gcc -c -g ScanDirOper.c
gcc -g -o release/TestScanDir ScanDirOper.o
rm *.o
3. 程式執行結果
轉到release目錄下,輸入“TestScanDir”,執行結果如下:
zhou@linux:~/zhouzx/ScanDirOper/release> TestScanDir
The amount of scanded file is:3
The scanded file 1 is:Test_1.txt
The scanded file 2 is:Test_2.txt
The scanded file 3 is:Test_3.txt
對照圖2可知,滿足條件的檔案被全部掃描出來了。
本文用一個實際的C程式為例來說明了目錄掃描函數(scandir)的使用方法。總的說來,該函數在Linux相關函數操作中使用得比較的廣泛,大家一定要學會其用法。
(本人微網誌:http://weibo.com/zhouzxi?topnav=1&wvr=5,微信号:245924426,歡迎關注!)