ls命令簡單實現 目錄讀寫

2021-09-29 08:47:52 字數 744 閱讀 4456

unix/linux中的目錄讀寫,類似於檔案讀寫,使用系統呼叫opendir開啟,readdir將檔案的一些資訊寫入乙個結構體中。

man -k direct |

grep

read

出現系統呼叫readdir() - read a directory entry(檢視檔案記錄),大概是我們想要的。man readdir開啟看看

果然有結構體dirent 和標頭檔案的相關資訊,然後考慮如何開啟開啟目錄,同樣使用man檢索出opendir,結合

開啟-讀取-輸出顯示-關閉的基本思路完成ls簡版實現。

上面說的還是過於模糊。直接些說,目錄操作類似於檔案操作,

使用的標頭檔案有

1、首先使用opendir開啟目錄(獲取目錄指標)

原型:dir* opendir(const

char

*)2、然後使用readdir

()讀取目錄

原型:struct dirent* readdir(dir*)

3、最後關閉 closedir()

而這些都能在man手冊中查詢到。

簡單實現ls命令

include include include include include include include include include include int aflag,lflag typedef struct node linknode,linklist void display fil...

linux c 實現 ls 命令

include include include include include include ctime 函式 include 操作目錄 include 操作檔案 include 獲取檔案所有者 include infomodeall a infomodedetail l 顯示資訊模式 typed...

linux中目錄資訊查詢ls命令的簡單使用方法

文中截圖來自於centos 6.3系統 a 顯示包括隱藏檔案在內的所有檔案 l 顯示詳細資訊 ls l 命令等同於ll命令 d 檢視目錄的屬性 h 採用看k,m,gb等單位來顯示檔案大小 i 顯示inode 最前邊例如 rw r r 代表的是檔案的許可權,預設是十位 第一位如果是 代表的是檔案型別是...