Linux下的大部分文件/目录处理函数都需包含头文件:#include ,少部分需包含或
01 文件类型:
S_ISREG(st_mode)---普通文件
S_ISDIR(st_mode)---目录
S_ISCHR(st_mode)---字符设备
S_ISBLK(st_mode)---块设备
S_ISFIFO(st_mode)---管道或FIFO
S_ISLNK(st_mode)---符号链接
S_ISSOCK(st_mode)---套接字socket
02 文件访问权限
S_IRUSR---用户(所有者)读
S_IWUSR---用户(所有者)写
S_IXUSR---用户(所有者)执行
S_IRGRP---组读
S_IWGRP---组写
S_IXGRP---组执行
S_ROTH---其他读
S_IWOTH---其他写
S_IXOTH---其他执行
S_ISUID---执行设置用户ID
S_ISGID---执行设置组ID
S_ISVTX---粘着位
S_IRWXU表示1~3的和,S_IRWXG表示4~6的和,S_IRWXO表示7~9的和
一、Linux下文件和目录的处理函数,成功则返回0,出错则返回-1;
1 获取文件/目录的信息buf,比如st_mode, st_info, st_nlink, st_uid, st_gid, st_size, st_atime/st_mtime/st_ctime等
int stat(const char *pathname, struct stat *buf);
2 文件/目录的访问权限测试:按照实际用户ID和实际组ID测试,而open函数按照有效用户ID和有效组ID进行测试;---
int access(const char *pathname, int mode); //mode=F_OK、R_OK、W_OK、X_OK
3 改变文件/目录的访问权限
int chmod(const char *pathname, mode_t mode);
4 文件截断---
int truncate(const char *pathname, off_t length); //若length>文件现有长度则出现空洞;
5 文件的硬链接:目录不支持硬链接
int link(const char *existingpath, const char *newpath);---
int unlink(const char *pathname);---
int remove(const char *pathname); //对于文件等同于unlink,对于目录等同于rmdir;---
6 文件/目录的符号连接即软连接---
int symlink(const char *actualpath, const char *syspath);
size_t readlink(const char *pathname, char *buf, size_t bufsize); //成功则返回读取的字节数,出错则返回-1;
7 文件/目录的重命名---
int rename(const char *oldname, const char *newname);
8 目录操作
(1)创建和销毁
int mkdir(const char *pathname, mode_t mode); //
int rmdir(const char *pathname); //只能删除空目录,若非空则需要先unlink删除文件,再调用rmdir删除空目录;
(2)目录遍历---
DIR *opendir(const char *pathname);
struct dirent *readdir(DIR *dp);
int closedir(DIR *dp);
long telldir(DIR *dp); //返回与dp关联的目录的当前位置;
void seekdir(DIR *dp, long loc);
void rewinddir(DIR *dp);
9 修改进程的工作目录---
int chdir(const char *pathname);