檔案和目錄

2021-08-05 23:10:01 字數 4030 閱讀 4461

學習筆記,小白可以相互學習,大佬看到能告訴咱理解不對的地方就好了。

函式

1.stat

獲取檔案/目錄屬性資訊

#include

#include

#include

int stat(const char *path, struct stat *buf);     //主要掌握這乙個,函式返回乙個與此命名有關的資訊結構

int fstat(int fd, struct stat *buf);

int lstat(const char *path, struct stat *buf);

struct stat ;

struct passwd *uid = getpwuid(b.st_uid); //獲得使用者名稱

struct group *gid = getgrgid(b.st_gid);//獲得所屬組名

struct tm *t = localtime(&b.st_ctime);//獲取時間

printf("%2d月,%2d日,%2d點,%2d分,%2d秒  ",t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);

/**************自己寫的實現ls -l功能*******************************/

#include#include#include#include#include#include#include#include#includeint main()

struct passwd *uid = getpwuid(b.st_uid); //獲得使用者名稱

struct group *gid = getgrgid(b.st_gid);//獲得所屬組名

printf("userid:%s ",uid->pw_name);

printf("groupid:%s ",gid->gr_name);

//******************檔案型別**************

switch(b.st_mode & s_ifmt)//檔案型別的位遮罩s_tfmt

//***********************檔案的許可權**************

(b.st_mode & s_irusr?printf("r"):printf("-"));

(b.st_mode & s_iwusr?printf("w"):printf("-"));

(b.st_mode & s_ixusr?printf("x"):printf("-"));

(b.st_mode & s_irgrp?printf("r"):printf("-"));

(b.st_mode & s_iwgrp?printf("w"):printf("-"));

(b.st_mode & s_ixgrp?printf("x"):printf("-"));

(b.st_mode & s_iroth?printf("r"):printf("-"));

(b.st_mode & s_iwoth?printf("w"):printf("-"));

(b.st_mode & s_ixoth?printf("x "):printf("- "));

//**********檔案大小************************

printf("%5ld ",b.st_size);

//************時間*******************************

struct tm *t = localtime(&b.st_ctime);

printf("%2d月,%2d日,%2d點,%2d分,%2d秒 ",t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);

//************檔案名字***************

printf("%s",r->d_name);

printf("\n");

}closedir(f);

return 0;

}

/*******************標準點的********************************/

#include #include #include #include #include #include #include #include #include #include void getfiletype(mode_t m, char *type)

void getfilemode(mode_t m, char *mode)

void getfileusrname(uid_t uid, char *usrname)

void getfilegrpname(gid_t gid, char *grpname)

void getfiletime(time_t ctime, char *filetime)

int main()

char type;

getfiletype(buf.st_mode, &type);

char mode[9] = ;

memset(mode, '-', sizeof(mode));

getfilemode(buf.st_mode, mode);

char usrname[10];

getfileusrname(buf.st_uid, usrname);

char grpname[10];

getfilegrpname(buf.st_gid, grpname);

char filetime[20];

getfiletime(buf.st_ctime, filetime);

fprintf(stdout, "%c%9s %2d %10s %10s %6ld %15s %s\n", type, mode, buf.st_nlink, usrname, grpname, buf.st_size, filetime, file->d_name);

} return 0;

}

2.opendir

開啟目錄

#include

#include

dir *opendir(const char *name);

成功返回dir*型別的目錄流,失敗返回null

3.readdir

讀取目錄

#include

struct dirent *readdir(dir *dirp);

成功返回下個目錄進入點,失敗或者到達目錄檔案尾返回null

struct dirent ;

dir *f = opendir(".");//開啟當前目錄

struct dirent *r;

struct stat b;

while(r = readdir(f)) //讀取目錄

//目錄下的成員名

4.closedir

關閉目錄

#include

#include

int closedir(dir *dirp);

關閉成功返回0,失敗返回-1,並設定errno

檔案和目錄

include include include include 判斷目錄或檔案是否存在 bool isfiledirexist string path while bsearchfinished tmppath fd.cfilename tmpdestpath destdir if tmpdestp...

檔案和目錄

1 與乙個程序相關聯的id 有6個或者更多。實際 有效 儲存的設定。設定使用者id 和設定組id 當執行乙個程式檔案時,程序的有效使用者id通常就是實際的使用者id,有效組id通常就是實際的組id。但是,可以在在檔案模式字 st mode 中設定乙個特殊的標誌,其含義是 當執行此檔案時,將程序的有效...

檔案和目錄

include include 該標頭檔案是為了使用opendir,readdir函式的 include include int main int argc,char argv opendir返回的是指向dir結構的指標,我們將該指標傳送給readdir 函式,不用關心dir結構中包含了什麼,然後迴...