Linux 檔案處理 之掃瞄目錄 DIR

2021-09-07 21:15:14 字數 4427 閱讀 4852

§

3.8掃瞄目錄

linux

系統上乙個常見問題就是對目錄進行掃瞄,也就是確定乙個特定目錄下存放的檔案。在

shell

程式設計中,這很容易做到——只需讓

shell

做一次表示式的萬用字元擴充套件。過去,

unix

作業系統的各種變體都允許使用者通過程式設計訪問底層檔案系統結構。我們仍然可以把目錄當作乙個普通檔案那樣開啟,並直接讀取目錄資料項,但不同的檔案系統結構及其實現方法已經使這種辦法沒什麼可移植性了。現在,一整套標準的庫函式已經被開發出來,使得目錄的掃瞄工作變得簡單多了。

與目錄操作有關的函式在

dirent.h

標頭檔案中宣告。它們把乙個名為

dir的結構作為目錄操作的基礎。被稱為「目錄流」的指向這個結構的指標(

dir *

)被用來完成各種目錄操作,其使用方法與檔案流(

file *

)非常相似。目錄資料項本身在

dirent

結構中返回,該結構也是在

dirent.h

標頭檔案裡宣告的,使用者絕不要直接改動

dir結構中的資料字段。

我們將介紹下面這幾個函式:

?opendir

、closedir

?readdir

?telldir

?seekdir

opendir函式

opendir

函式的作用是開啟乙個目錄並建立乙個目錄流。如果成功,它返回乙個指向

dir結構的指標,該指標用於讀取目錄資料項。

#include

#include

dir *opendir(const char *name);

opendir

在失敗時會返回乙個空指標。注意,目錄流用乙個底層檔案描述符來訪問目錄本身,所以如果開啟的檔案過多,

opendir

可能會失敗。

readdir函式

readdir

函式將返回乙個指標,指標指向的結構裡儲存著目錄流

drip

中下乙個目錄項的有關資料。後續的

readdir

呼叫將返回後續的目錄項。如果發生錯誤或者到達目錄尾,

readdir

將返回null

。posix

相容的系統在到達目錄尾時會返回

null

,但並不改變

errno

的值,在發生錯誤時才會設定

errno。

#include

#include

struct dirent *readdir(dir *dirp);

注意,如果在

readdir

函式掃瞄目錄的同時還有其他程序在該目錄裡建立或刪除檔案,

readdir

將不保證能夠列出該目錄裡的所有檔案(和子目錄)。

dirent

結構中包含的目錄資料項內容包括以下部分: ?

ino_t d_ino

——檔案的

inode

節點號。 ?

char d_name

——檔案的名字。

要想進一步了解目錄中某個檔案的詳細資料,則需要使用在本章前面介紹過的

stat

呼叫。

telldir函式

telldir

函式的返回值記錄著乙個目錄流裡的當前位置。你可以在隨後的

seekdir

呼叫中利用這個值來重置目錄掃瞄到當前位置。

#include

#include

long int telldir(dir *dirp);

seekdir函式

seekdir

函式的作用是設定目錄流

dirp

的目錄項指標。

loc的值用來設定指標位置,它應該通過前乙個

telldir

呼叫獲得。

#include

#include

void seekdir(dir *dirp, long int loc);

closedir函式

closedir

函式關閉乙個目錄流並釋放與之關聯的資源。它在執行成功時返同

0,發生錯誤時返回-1。

#include

#include

int closedir(dir *dirp);

在下面的

printdir.c

程式中,我們把許多檔案處理函式集中在一起實現乙個簡單的目錄列表功能。目錄中的每個檔案單獨列在一行上。每個子目錄會在它的名字後面加上乙個斜線字元

/,子目錄中的檔案在縮排四個空格後依次排列。

程式會逐個切換到每個下級子目錄裡,這樣使它找到的檔案都有乙個可用的名字,也就是說,它們都可以被直接傳遞到

opendir

函式裡去。如果目錄的巢狀結構太深,程式執行就會失敗,因為對允許開啟的目錄流數目是有限制的。

我們當然可以採取乙個更通用的做法,讓程式能夠通過乙個命令列引數來指定起點(從哪個目錄開始)。請查閱有關工具程式(如ls和

find

)的linux

源**來找到實現更通用程式的方法。

實驗:乙個目錄掃瞄程式

# cat printdir.c

which prints out the current directory.

it will recurse for subdirectories, using the depth parameter is used for indentation.*/

#include

#include

#include

#include

#include

#include

void printdir(char *dir, int depth)

chdir(dir);

while((entry = readdir(dp)) != null)

else printf("%*s%s\n",depth,"",entry->d_name); }

chdir("..");

closedir(dp); }

/*now we move onto the main function.*/

int main()

cat printdir2.c

which prints out the current directory.

it will recurse for subdirectories, using the depth parameter is used for indentation.*/

#include

#include

#include

#include

#include

#include

void printdir(char *dir, int depth)

chdir(dir);

while((entry = readdir(dp)) != null)

else printf("%*s%s\n",depth,"",entry->d_name); }

chdir("..");

closedir(dp); }

/*now we move onto the main function.*/

int main(int argc, char* ar**)

檔案操作之3 掃瞄目錄

用到的系統呼叫函式如下 lstat,opendir readdir chdir函式 1.lstat函式 函式原型 include include include int lstat const char path,struct stat buf st mode 檔案許可權和檔案型別資訊 st ino...

Linux 檔案 目錄 處理命令

檔案處理命令 新建檔案 檔案不存在,則新建,存在則覆蓋 touch 檔名 檔案不存在,則不新建,相當於 no create touch c 檔名 修改檔案最後一次修改時間 時間格式可以是 mmddhhmm 年的值預設用當前年 touch c t 時間 檔名 刪除檔案 rm rf 檔名 修改檔案 讓檔...

linux目錄檔案處理命令

目錄處理命令 ls 顯示目錄檔案 a 所有檔案 l 詳細顯示 d 檢視屬性 mkdir 建立新目錄 p 遞迴建立 cd 切換目錄 cd 返回上一級 pwd 顯示當前所在目錄 rmdir 刪除空目錄 rm 刪除檔案 r 刪除目錄 f 強制刪除 rm rf 全部刪除 cp 複製檔案或目錄 語法 cp r...