日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

十三、linux编程中目录IO常用编程函数

發布時間:2025/4/5 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 十三、linux编程中目录IO常用编程函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概念:

????????索引節點,Inode是Index Node的縮寫,存儲于文件系統上的任何文件都可以用索引節點來表示,所以也可以說索引節點是整個linux文件系統的基礎。操作系統在讀取硬盤的時候不是一個塊一個塊的來讀取信息,因為這樣做的話效率太低,文件數據都儲存在“塊”中,那么很顯然,我們還必須找到一個地方儲存文件的元信息,比如文件的創建者、文件的創建日期、文件的大小等等。這種儲存文件元信息的區域就叫做inode,中文譯名為“索引節點”。

????????在Linux系統中,文件系統主要分為兩部分,一部分為元數據(metadata),另一部分為數據本身。元數據,換句話說,就是包含了數據有關信息的數據。索引節點就管理著文件系統中元數據的部分。

一、stat函數組(獲取文件信息,具體看stat結構體 )

1、頭文件、函數原型及相關結構體(可以通過man 2 stat 打開對應的stat幫助文檔)

/*所需頭文件*/ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> /*函數原型*/ int stat(const char *path, struct stat *buf); – 參數*path:文件路徑 – 參數*buf:文件信息 – 返回值:成功為0,否則為-1 int fstat(int fd, struct stat *buf); – 參數fd:文件描述符 – 參數*buf:文件信息 – 返回值:成功為0,否則為-1 int lstat(const char *path, struct stat *buf); – 參數*path:文件路徑 – 參數*buf:返回文件的信息,針對符號鏈接,lstat 返回鏈接本身,而不是而非目標文件 – 返回值:成功為0,否則為-1stat結構體包含對象:struct stat {dev_t st_dev; /* ID of device containing file */ino_t st_ino; /* inode number */mode_t st_mode; /* protection */nlink_t st_nlink; /* number of hard links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */dev_t st_rdev; /* device ID (if special file) */off_t st_size; /* total size, in bytes */blksize_t st_blksize; /* blocksize for file system I/O */blkcnt_t st_blocks; /* number of 512B blocks allocated */time_t st_atime; /* time of last access */time_t st_mtime; /* time of last modification */time_t st_ctime; /* time of last status change */};

2、具體使用方法及代碼:?

#include <stdio.h> //通過man文檔可以查看到stat函數組頭文件 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>//open函數的參數頭文件 #include <fcntl.h>int main(int argc,char *argv[]) {struct stat groupstat;int fd,ret;if(argc <2){printf("\nPlease input file path\n");return 1;}//stat函數測試 ret = stat(argv[1],&groupstat);if(ret){printf("Please make sure file path\n");return 1;}printf("stat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino);//fstat函數測試fd = open(argv[1],O_RDWR|O_NOCTTY|O_NDELAY);if(fd<0){printf("Please make sure file path\n");return 1;}ret = fstat(fd,&groupstat);if(ret){printf("Please make sure file path\n");return 1;}printf("fstat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino);//lstat函數測試 ret = lstat(argv[1],&groupstat);if(ret){printf("Please make sure file path\n");return 1;}printf("lstat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino);return 0; }

二、chmod函數組(設置文件權限 )

1、頭文件、函數原型及相關結構體(可以通過man 2 chmod打開對應的chmod幫助文檔)

/*頭文件*/ #include <sys/stat.h> /*函數原型*/ int chmod(const char *path, mode_t mode);// 修改指定路徑的文件權限 – 參數*path:文件路徑。 – 參數mode:直接使用數字即可。和前面命令中chmod 777 xxx 中的777 這個參數含義類似,也可以使用文檔中的組合值。 – 返回值:成功返回0,錯誤返回-1。 int fchmod(int fd, mode_t mode);// 修改打開的文件權限 – 參數fd:文件描述符。 – 參數mode:直接使用數字即可。和前面命令中chmod 777 xxx 中的777 這個參數含義類似,也可以使用文檔中的組合值。 – 返回值:成功返回0,錯誤返回-1。

2、具體使用方法及代碼:?

#include <stdio.h>#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int main(int argc,char *argv[]) {int fd,ret;if(argc <3){printf("\nPlease input file path\n");return 1;}//chmod函數測試 ret = chmod(argv[1],0777);if(ret<0){printf("Please make sure file path\n");return 1;}printf("chmod %s 0777 is success!\n",argv[1]);//fchmod函數測試 fd = open(argv[2],O_RDWR|O_NOCTTY|O_NDELAY);if(fd<0){printf("Please make sure file path\n");return 1;}ret = fchmod(fd,0555);if(ret<0){printf("Please make sure file path\n");return 1;}printf("fchmod %s 0555 is success!\n",argv[1]);return 0; }

三、獲取當前目錄

1、頭文件、函數原型及相關結構體(可以通過man 3 getcwd打開對應的getcwd幫助文檔)

/*頭文件*/ #include <unistd.h> /*函數原型*/ char *getcwd(char *buf, size_t size); – 參數*buf:保存當前目錄的緩沖區 – 參數size:在現代linux 中,buf 的長度至少可以為255 字節 – 返回值:成功返回指向當前目錄的指針,和buf 的值一樣,錯誤返回NULL char *getwd(char *buf); – 參數*buf:保存當前目錄的緩沖區 – 返回值:成功返回指向當前目錄的指針,和buf 的值一樣,錯誤返回NULL char *get_current_dir_name(void); – 參數:無 – 返回值:成功返回指向當前目錄的指針,錯誤返回NULL

2、具體使用方法及代碼:?

#include <stdio.h> /*getcwd、getwd和get_current_dir_name 函數的頭文件*/ #define __USE_GNU #include <unistd.h>#define LENTH 255 int main() {char pwd[LENTH];char *wd; //getcwd函數測試 if(!getcwd(pwd,LENTH)){perror("getcwd");return 1;}printf("\ngetcwd pwd is %s\n",pwd);//getwd函數測試wd = getwd(pwd);if(!wd){perror("getcwd");return 1;}printf("\ngetwd pwd is %s\n",wd);//get_current_dir_name函數測試 wd = get_current_dir_name();if(!wd){perror("getcwd");return 1;}printf("\nget_current pwd is %s\n",wd);return 0; }

四、創建目錄

1、頭文件、函數原型及相關結構體(可以通過man 2 mkdir打開對應的mkdir幫助文檔)

/*頭文件*/ #include <sys/stat.h> #include <sys/types.h> /*函數原型*/ int mkdir(const char *pathname, mode_t mode); – 參數:文件路徑 – 參數mode:直接使用數字即可。和前面命令中chmod 777 xxx 中的777 這個參數含義類似,也可以使用文檔中的組合值。 – 返回值:成功返回0,錯誤返回-1。

2、具體使用方法及代碼:??

#include <stdio.h> //mkdir函數頭文件 #include <sys/stat.h> #include <sys/types.h>int main(int argc,char *argv[]) {int ret;//檢測參數 if(argc <2){printf("\nPlease input file path\n");return 1;} //使用mkdir函數新建目錄ret = mkdir(argv[1],0777);if(ret<0){printf("mkdir %s failed!\n",argv[1]);return 1;}printf("mkdir %s suceces!\n",argv[1]);return 0; }

五、刪除目錄

1、頭文件、函數原型及相關結構體(可以通過man 2 rmdir打開對應的rmdir幫助文檔)

/*頭文件*/ #include <unistd.h> /*函數原型*/ int rmdir(const char *pathname); – 參數*pathname:文件和目錄的路徑 – 返回值:成功返回0,錯誤返回-1

2、具體使用方法及代碼:??

#include <stdio.h> //rmdir函數頭文件 #include <unistd.h>int main(int argc,char *argv[]) {int ret;//檢測參數 if(argc <2){printf("\nPlease input file path\n");return 1;} //使用rmdir函數刪除目錄ret = rmdir(argv[1]);//目錄要是絕對目錄if(ret<0){printf("rmdir %s failed!\n",argv[1]);return 1;}printf("rmdir %s suceces!\n",argv[1]);return 0; }

六、改變當前工作目錄

1、頭文件、函數原型及相關結構體(可以通過man 2 chdir打開對應的chdir幫助文檔)

在實際應用中,代碼可能需要從當前目錄中進到其它目錄,這個時候首先需要使用 getcwd函數獲取當前目錄,保存起來,然后使用 chdir 跳到其它目錄,完成操作,然后再使用 chdir回到最初保存的目錄。

#include <unistd.h> /*函數原型*/ int chdir(const char *path);- 參數 *path:文件路徑- 返回值:成功返回 0,錯誤返回-1。 int fchdir(int fd);- 參數 fd:open 函數返回的句柄,文件描述符。- 返回值:成功返回 0,錯誤返回-1。

2、具體使用方法及代碼:?

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> //chdir和fchdir函數頭文件 #include <unistd.h>#define LENTH 255int main(int argc,char *argv[]) {int ret;char pwd[LENTH];//檢測參數 if(argc <3){printf("\nPlease input file path\n");return 1;}//getcwd函數獲取當前目錄 if(!getcwd(pwd,LENTH)){perror("getcwd");return 1;}printf("\ngetcwd pwd is %s\n",pwd);//使用chdir函數轉入其他目錄ret = chdir(argv[1]);if(ret){printf("Please make sure file path\n");return 1;}printf("chdir %s is success!\n",argv[1]);//轉入其他目錄,完成操作 //使用rmdir函數刪除目錄ret = rmdir(argv[2]);if(ret<0){printf("rmdir %s failed!\n",argv[2]);return 1;}printf("rmdir %s is success!\n",argv[2]);//再次使用chdir回到pwd保存的目錄ret = chdir(pwd);if(ret){printf("Please make sure file path\n");return 1;}printf("chdir %s is success!\n",pwd);return 0; }

七、opendir和closedir目錄

1、頭文件、函數原型及相關結構體(可以通過man 3 opendir(closedir)打開對應的opendir(closedir)幫助文檔)

前面介紹open和close函數用于打開關閉文件,這里介紹的opendir和closedir用于打開目錄,相當于ls命令。

#include <sys/types.h> #include <dirent.h> /*函數原型*/ DIR *opendir(const char *name); – 參數:目錄的路徑。 – 返回值:成功返回指向目錄流的指針,錯誤返回NULL int closedir(DIR *dirp); – 參數:opendir 返回的dir 指針 – 返回值:成功返回0, 失敗返回-1

2、具體使用方法及代碼:?

#include <stdio.h> //opendir和closedir函數頭文件 #include <dirent.h> #include <sys/types.h>int main(int argc,char *argv[]) {int ret;DIR *dir;//檢測參數 if(argc <2){printf("\nPlease input file path\n");return 1;} //使用opendir函數打開目錄dir = opendir(argv[1]);if(dir==NULL){printf("opendir %s failed!\n",argv[1]);return 1;}printf("opendir %s suceces!\n",argv[1]); //使用closedir函數關閉目錄closedir(dir);return 0; }

八、readdir讀取目錄信息

1、頭文件、函數原型及相關結構體(可以通過man 3 readdir打開對應的readdir幫助文檔)

在前面使用opendir打開目錄的基礎上,可以使用readdir讀取目錄信息。

#include <dirent.h> /*函數原型*/ struct dirent *readdir(DIR *dirp); – 參數dirp:opendir 函數打開目錄后返回的文件指針。 – 返回值:成功返回指向dirp 的指針dirent ,錯誤返回NULL。struct dirent {ino_t d_ino; /* inode number */off_t d_off; /* offset to the next dirent */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file; not supportedby all file system types */char d_name[256]; /* filename */};

2、具體使用方法及代碼:??

#include <stdio.h> //opendir,closedir,readdir函數頭文件 #include <dirent.h> #include <sys/types.h>int main(int argc,char *argv[]) {int ret;DIR *dir;struct dirent *catlog; //檢測參數 if(argc <2){printf("\nPlease input file path\n");return 1;} //使用opendir函數打開目錄dir = opendir(argv[1]);if(dir==NULL){printf("opendir %s failed!\n",argv[1]);return 1;}printf("opendir %s suceces!\n",argv[1]); //使用readdir讀取目錄argv[1]catlog = readdir(dir);if(catlog == NULL){printf("readdir %s failed!\n",argv[1]);return 1;}printf("%s d_ino is %ld\n ",argv[1],catlog->d_ino); //使用closedir函數關閉目錄closedir(dir);return 0; }

九、硬鏈接和軟鏈接(符號鏈接)

1、硬鏈接

????????在Linux系統中,多個文件名指向同一索引節點(Inode)是正常且允許的。一般這種鏈接就稱為硬鏈接。硬鏈接的作用之一是允許一個文件擁有多個有效路徑名,這樣用戶就可以建立硬鏈接到重要的文件,以防止“誤刪”源數據。(換句話說就是我們前面講得,文件的屬性和數據是分開的,將兩份不一樣的文件屬性同時指向同一份文件數據。那么這兩份文件屬性就是硬鏈接。)

硬鏈接函數
? int link(const char *oldpath, const char *newpath)
– 參數*oldpath:已有的文件路徑。
– 參數*newpath:新建的硬鏈接文件路徑。
– 返回值:成功返回0,錯誤返回-1。

#include <stdio.h> //link函數頭文件 #include <unistd.h>int main(int argc,char *argv[]) {int ret;if(argc <3){printf("\nPlease input file path\n");return 1;}//測試link函數ret = link(argv[1],argv[2]);if(ret){printf("link failed");return 1;}printf("link %s to %s success!\n",argv[1],argv[2]);return 0; }

2、軟鏈接

????????軟鏈接(也叫符號鏈接),類似于windows系統中的快捷方式,與硬鏈接不同,軟鏈接就是一個普通文件,只是數據塊內容有點特殊,文件用戶數據塊中存放的內容是另一文件的路徑名的指向,通過這個方式可以快速定位到軟連接所指向的源文件實體。軟鏈接可對文件或目錄創建。

軟鏈接函數
? int symlink(const char *oldpath, const char *newpath)
– 參數*oldpath:已有的文件路徑
– 參數*newpath:新建的符號鏈接文件路徑
– 返回值:成功返回0,錯誤返回-1

#include <stdio.h> //symlink函數頭文件 #include <unistd.h>int main(int argc,char *argv[]) {int ret;if(argc <3){printf("\nPlease input file path\n");return 1;}//測試symlink函數ret = symlink(argv[1],argv[2]);if(ret){printf("symlink failed");return 1;}printf("symlink %s to %s success!\n",argv[1],argv[2]);return 0; }

3、解除鏈接

解除鏈接函數
? int unlink(const char *pathname)
– 參數*pathname:鏈接文件的路徑
– 返回值:成功返回0,錯誤返回-1
– unlink指向軟鏈接,刪除軟鏈接;指向最后一個硬鏈接,相當于刪除文件。因為硬鏈接的文件數據至少要有一個硬鏈接,如果最后一個都被刪除,那么相當于文件數據也要刪除了。

#include <stdio.h> //unlink函數頭文件 #include <unistd.h>int main(int argc,char *argv[]) {int ret;if(argc <2){printf("\nPlease input file path\n");return 1;}//測試unlink函數ret = unlink(argv[1]);if(ret){printf("unlink failed");return 1;}printf("unlink %s is success!\n",argv[1]);return 0; }

十、文件拷貝

????????linux中,沒有專用的文件拷貝函數,而是通過從舊文件讀取(read函數),寫入(write函數)新文件,來實現文件的拷貝。

#include <stdio.h>#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>//argv[1] is oldpath ; argv[2] is newpath #define LENTH 1024 int main(int argc,char *argv[]) {int fds,fdt;char buffer[LENTH];char *fileold,*filenew;fileold = argv[1];filenew = argv[2];if(argc <3){printf("\nPlease input file path\n");return 1;}//打開oldpath fds = open(fileold,O_RDWR);if(fds<0){printf("Please make sure file path\n");return 1;}//打開newpath,如果沒有則創建目標文件fdt = open(filenew,O_WRONLY|O_CREAT);if(fdt<0){printf("Please make sure file path\n");return 1;}//讀和寫操作while(read(fds,buffer,LENTH)){write(fdt,buffer,strlen(buffer));}//關閉文件close(fds);close(fdt);printf("cp to finished!\n");printf("cp %s to %s success!\n",fileold,filenew);return 0; }

十一、文件移動

移動文件命令為mv,函數為rename。

文件移動函數:

int rename(const char *oldpath, const char *newpath)
– 參數*oldpath:舊的文件路徑
– 參數*newpath:新的文件路徑
– 返回值:成功返回0,錯誤返回-1

#include <stdio.h>#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>//argv[1] is oldpath ; argv[2] is newpath int main(int argc,char *argv[]) {int ret;if(argc < 3){printf("\nPlease input file path\n");return 1;}if(ret = rename(argv[1],argv[2])){printf("\nerr\n");}printf("rename %s to %s success!\n",argv[1],argv[2]);return 0; }

總結

以上是生活随笔為你收集整理的十三、linux编程中目录IO常用编程函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日本黄色美女视频 | 一区二区自拍偷拍 | 日韩永久免费视频 | 欧美成人午夜免费视在线看片 | 欧洲中文字幕日韩精品成人 | 伊人性视频 | 青青草伊人久久 | 91爱爱·com| 麻豆成人精品国产免费 | 日本一区二区观看 | 黄色片aaaa | 日韩精品一区二区三区在线播放 | 特级西西444www高清大胆免费看 | 无码一区二区三区在线 | 伊人天堂网 | 调教女m荡骚贱淫故事 | 推特裸体gay猛交gay | 精产国品一区二区三区 | 中文字幕超清在线观看 | 夜夜夜操 | 波多野结衣电车痴汉 | 青青国产在线视频 | 熟女人妻在线视频 | 国产精品夜夜夜爽阿娇 | 超碰伊人久久 | 不用播放器的av网站 | 国产香蕉一区二区三区 | 国产伦精品一区二区三区高清版禁 | 久久av一区二区三 | 亚洲最大成人综合网 | 岛国av一区二区三区 | 欧美操操| 黄色天堂| 91激情网 | 最新国产拍偷乱偷精品 | 亚洲91色| 综合网在线视频 | 91猎奇在线观看 | 超碰碰97| 久久国产秒 | 91一起草| 国产精品久久久久99 | 亚洲女人天堂网 | av在线观 | 日本黄色大片视频 | 日韩一二三区在线观看 | 国产精品99久久久久久宅男 | www.97av| 久久久久久久美女 | 性色在线视频 | 精品久久久久久亚洲综合网站 | 中文字幕在线播放av | 中文字幕有码av | 日韩a级在线观看 | 影音先锋波多野结衣 | 日韩中文一区 | 国产乱人乱偷精品视频a人人澡 | 成人网页 | 激情视频免费观看 | 波多野结衣在线免费观看视频 | 精品处破女学生 | 蜜桃传媒一区二区亚洲 | 国产精品免费电影 | 欧美成人tv | 综合一区二区三区 | 网站一区二区 | 日韩电影精品 | 伊人网色 | 黄色aaaa| 亚洲精品视频免费 | 亚欧色视频| 日本内谢少妇xxxxx少交 | 免费av网址在线 | 美女久久久久久久久 | av毛片观看 | 国产山村乱淫老妇女视频 | 国产精品99精品无码视 | 中日韩欧美在线观看 | 9久久9毛片又大又硬又粗 | 黄页在线观看 | 成人免费毛片高清视频 | 天天天干干干 | 亚洲男人天堂影院 | 九九黄色大片 | 国产一区二区黑人欧美xxxx | 国产激情网址 | 色爱av综合网 | 奇米久久 | 欧美女人一区二区 | 黄色国产在线观看 | 国产高清一 | 国产一级二级av | av网站国产 | 日日日日日日 | 国产成人精品免费视频 | 欧美大片网站 | 性日韩| 深夜影院在线观看 | 国产精品com|