您的位置:首页 > 其它

系统文件操作进一步

2015-12-14 20:57 253 查看
file_advance

access

int access(cosnt char *pathname, int mode);

mode: R_OK | W_OK | X_OK 或 F_OK测试文件存在性。

stat

#include <sys/stat.h>

#include <unistd.h>


int stat(const char *file_name, struct stat *buf);


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


struct stat {

dev_t st_dev; /* 设备 */

ino_t st_ino; /* 节点 */

mode_t st_mode; /* 模式 */

nlink_t st_nlink; /* 硬连接 */

uid_t st_uid; /* 用户 ID */

gid_t st_gid; /* 组 ID */

dev_t st_rdev; /* 设备类型 */

off_t st_off; /* 文件字节数 */

unsigned long st_blksize; /* 块大小 */

unsigned long st_blocks; /* 块数 */

time_t st_atime; /* 最后一次访问时间 */

time_t st_mtime; /* 最后一次修改时间 */

time_t st_ctime; /* 最后一次改变时间(指属性) */


};

stat用来判断没有打开的文件,而fstat用来判断打开的文件。使用最多的属性是st_mode,用下面的宏判断:

S_ISLNK(st_mode):是否是一个连接;

S_ISREG(st_mode):是否是一个常规文件;

S_ISDIR(st_mode):是否是一个目录;

S_ISCHR(st_mode):是否是一个字符设备;

S_ISBLK(st_mode):是否是一个块文件;

S_ISFIFO(st_mode):是否是FIFO文件;

S_ISSOCK(st_mode):是否是SOCKET文件。

目录操作

char *getcwd(char *buffer, size_t size);


int mkdir(const char *path, mode_t mode);


DIR *opendir(const char *path);


struct dirent *readdir(DIR *dir);


void rewinddir(DIR *dir);


void seekdir(DIR *dir, off_t off);


int closedir(DIR *dir);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: