linux下文件操作需要关注的函数


前述:
本文并不是为了详细讲解每个函数的细节,细节可参考man手册或者《unix环境高级编程》


一、打开或者创建文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

注:仅当创建新文件时才使用mode_t参数,mode_t受文件模式创建屏蔽字影响,可参考umask命令或umask函数。


二、关闭文件
#include <unistd.h>
int close(int fd);

注:关闭一个文件时还会释放该进程加在该文件上的所有记录锁。当一个进程终止时,内核自动关闭它所有的打开文件。


三、设置一个打开文件的偏移量
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);

注:lseek仅将当前的文件偏移量记录在内核,它并不引起任何I/O操作。文件偏移量可以大于文件的当前长度,在这种情况下,对该文件的下一次写将加长该文件,并在文件中构成一个空洞,文件中的空洞并不要求在磁盘上占用存储区。


四、读文件

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);

注:读数据成功会使偏移量增加实际读的字节数。


五、写文件
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

注:写数据成功会使偏移量增加实际写的字节数。


六、多线程下的读写原子性
#include <unistd.h>
ssize_t pread(int fd, void *buf, size_t count, off_t offset);
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

注:本质上就是lseek与read/write结合了,可参考文章:http://blog.csdn.net/daiyudong2020/article/details/52015817


七、复制文件描述符
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);

注:返回的新文件描述符与参数oldfd共享同一个文件表项


八、冲洗内核缓冲区
#include <unistd.h>
void sync(void);
int fsync(int fd);

注:sync只是将修改过的块缓冲区排入写队列,然后就返回,并不等待实际写磁盘操作结束。fsync函数只对由文件描述符fd指定的一个文件起作用,并且等待写磁盘操作结束才返回。fsync可用于数据库这样的应用程序。


九、改变已经打开文件的属性
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );

注:fcntl函数有以下五个功能
1.复制一个已有的描述符
2.获取/设置文件描述符标志位
3.获取/设置文件状态标志位(cmd=F_GETFL或F_SETFL), 最常用的就是套接字设置非阻塞或者阻塞
4.获取/设置异步I/O所有权
5.获取/设置记录锁


十、获取文件的信息结构
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

注:使用stat函数最多的地方就是ls -l命令,用其可以获得有关一个文件的所有信息。stat结构中的大多数信息都取自i节点,文件名和i节点编号取自目录项中


十一、测试文件的访问权限
#include <unistd.h>
int access(const char *pathname, int mode);

注:如果测试文件是否已经存在mode就为F_OK,否则mode就是R_OK/W_OK/X_OK的位或


十二、设置进程的文件创建屏蔽字

#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t mask);

注:假设open时指定mode是0777,而当前屏蔽字是0022,那么创建成功的文件就是0755

十三、更改文件的访问权限

#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);


十四、更改文件的用户ID和组ID
#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);


十五、文件截断
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);


十六、删除文件
#include <stdio.h>
int remove(const char *pathname);

注:只有当进程关闭该文件或者进程终止时,该文件的内容才会被真正删除。


十七、文件重命名
#include <stdio.h>
int rename(const char *oldpath, const char *newpath);

注:文件或者目录都适用


十八、创建和读取符号链接
#include <unistd.h>
int symlink(const char *oldpath, const char *newpath);

#include <unistd.h>
ssize_t readlink(const char *path, char *buf, size_t bufsiz);

注:参数要用绝对路径,创建函数symlink的功能类似ln命令。读取函数readlink组合了open/read/close的所有操作。


十九、创建和删除目录

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

#include <unistd.h>
int rmdir(const char *pathname);

注:mkdir创建目录,rmdir删除目录


二十、更改进程的当前工作目录
#include <unistd.h>
int chdir(const char *path);
int fchdir(int fd);

注:每个进程都有一个当前工作目录,工作目录是进程的一个属性,可通过lsof查看cwd项


二十一、文件的访问和修改时间更改

#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]);



End


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值