apue 第4章 文件和目录
生活随笔
收集整理的這篇文章主要介紹了
apue 第4章 文件和目录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取文件屬性
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h>int stat(const char *pathname, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *pathname, struct stat *buf);#include <fcntl.h> #include <sys/stat.h>int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags);返回值:成功0,出錯-1
stat結構:
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 filesystem I/O */blkcnt_t st_blocks; /* number of 512B blocks allocated */ /* Since Linux 2.6, the kernel supports nanosecondprecision for the following timestamp fields.For the details before Linux 2.6, see NOTES. */struct timespec st_atim; /* time of last access */ struct timespec st_mtim; /* time of last modification */ struct timespec st_ctim; /* time of last status change */ #define st_atime st_atim.tv_sec /* Backward compatibility */ #define st_mtime st_mtim.tv_sec #define st_ctime st_ctim.tv_sec };
?
按實際用戶ID和實際組ID進行訪問權限測試
#include <unistd.h>int access(const char *pathname, int mode);#include <fcntl.h> #include <unistd.h>int faccessat(int dirfd, const char *pathname, int mode, int flags);返回值:成功0,出錯-1
?
為進程設置文件模式創建屏蔽字
#include <sys/types.h> #include <sys/stat.h>mode_t umask(mode_t mask); 返回值:之前文件模式創建屏蔽字?
更改現有文件的訪問權限
#include <sys/stat.h>int chmod(const char *pathname, mode_t mode); int fchmod(int fd, mode_t mode);#include <fcntl.h> #inlcude <sys/stat.h>int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);返回值:成功0,出錯-1
?
用于改變文件的用戶ID和組ID。如果兩個參數owner或group中的任意一個是-1,則對應ID不變
#include <unistd.h>int chown(const char *pathname, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int lchown(const char *pathname, uid_t owner, gid_t group);#include <fcntl.h> /* Definition of AT_* constants */ #include <unistd.h>int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);返回值:成功0,出錯-1
?
截斷文件
#include <unistd.h> #include <sys/types.h>int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); 返回值:成功0,出錯-1?
?
創建一個指向現有文件的鏈接
#include <unistd.h>int link(const char *oldpath, const char *newpath);#include <fcntl.h> /* Definition of AT_* constants */ #include <unistd.h>int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags); 返回值:成功0,出錯-1?
刪除一個現有目錄項
#include <unistd.h>int unlink(const char *pathname);#include <fcntl.h> #include <unistd.h>int unlinkat(int dirfd, const char *pathname, int flags);返回值:成功0,出錯-1
?
解除對一個文件或目錄的鏈接
#include <stdio.h>int remove(const char *pathname); 返回值:成功0,出錯-1?
對文件或目錄進行重命名
#include <stdio.h>int rename(const char *oldpath, const char *newpath);#include <fcntl.h> #include <stdio.h>int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags); 返回值:成功0,出錯-1?
創建一個符號鏈接
#include <unistd.h>int symlink(const char *target, const char *linkpath);#include <fcntl.h> #include <unistd.h>int symlinkat(const char *target, int newdirfd, const char *linkpath); 返回值:成功0,出錯-1?
因為open函數跟隨符號鏈接,所以需要一種方法打開鏈接本身,并讀該鏈接中的名字。
#include <unistd.h>ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);#include <fcntl.h> /* Definition of AT_* constants */ #include <unistd.h>ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz); 返回值:成功讀取字節數,出錯-1?
一個文件的訪問和修改時間可以用以下幾個函數更改。futimens和utimensat函數可以指定納秒級精度的時間戳。
用到數據結構是與stat函數族相同的timespec結構。
#include <fcntl.h> /* Definition of AT_* constants */ #include <sys/stat.h>int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags);int futimens(int fd, const struct timespec times[2]); 返回值:成功0,出錯-1?
創建目錄,刪除目錄
#include <sys/stat.h> #include <sys/types.h>int mkdir(const char *pathname, mode_t mode);#include <fcntl.h> /* Definition of AT_* constants */ #include <sys/stat.h>int mkdirat(int dirfd, const char *pathname, mode_t mode);返回值:成功0,出錯-1
?
刪除一個空目錄,空目錄只包含.和..
#include <unistd.h>int rmdir(const char *pathname); 返回值:成功0,出錯-1?
讀目錄
#include <sys/types.h> #include <dirent.h>DIR *opendir(const char *name); DIR *fdopendir(int fd);返回值:成功指針,出錯NULL struct dirent *readdir(DIR *dirp);
返回值:成功指針,若在目錄或出錯,返回NULL void rewinddir(DIR *dirp); int closedir(DIR *dirp);
返回值:成功0,出錯-1 long telldir(DIR *dirp);
返回值:dp關聯的目錄中的當前位置 void seekdir(DIR *dirp, long loc);
dirent結構
struct dirent {ino_t d_ino; /* inode number */off_t d_off; /* not an offset; see NOTES */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file; not supportedby all filesystem types */char d_name[256]; /* filename */ };?
?
?
更改當前目錄
#include <unistd.h>int chdir(const char *path); int fchdri(int fd);返回值:成功0,出錯-1
?
得到當前工作目錄完整絕對路徑名
#include <unistd.h>char *getcwd(char *buf, size_t size); 返回值:成功返回buf,出錯NULL?
轉載于:https://www.cnblogs.com/ch122633/p/8157707.html
總結
以上是生活随笔為你收集整理的apue 第4章 文件和目录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++获取系统当前时间
- 下一篇: 多个工作表同时处理+选中不连续的单元格和