一. Linux文件操作
文件简介:
什么是文件?
存储在外存上数据的集合;
Linux系统中文件是如何标识的?文件描述符(file descriptor): 是一个非负的正整数;
系统会为每一个应用程序默认打开3个文件描述符:
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
1. 文件操作方式
1.1 系统IO (设备文件访问)
access
头文件: #include <unistd.h>
#include <fcntl.h>
函数原型: int access(const char* path,int mode);
函数功能: 获取文件属性(存在性,可操作性)
函数参数: path: 目标文件的文件路径
mode: 判断模式: 取值如下:
R_OK 获取文件读权限
W_OK 获取文件写权限
X_OK 获取文件执行权限
F_OK 获取文件是否存在
返回值: 0: 确认模式是满足的
-1: 不满足:错误码存放在 errno.
2.文件的访问步骤:
2.1 打开文件,获取文件描述符
open
头文件: #include <unistd.h>
#include <fcntl.h>
函数原型: int open(const char* path,int flags,mode_t mode); //创建文件
int open(const char* path,int flags); //针对已存在文件
函数功能: 打开/创建文件
函数参数: path: 目标文件的文件路径
flags: 打开模式: 取值如下:
必选项:
O_RDONLY 只读模式
O_WRONLY 只写模式
O_RDWR 读写模式
附加项(常见):
O_CREAT : 往往与写操作连用,代表文件不存在时,需要创建
O_APPEND: 追加写
O_TRUNC: 覆盖写入
...
mode: 如果创建文件,用来指定新文件的权限.
返回值: 成功: 返回文件描述符
失败: 返回-1:错误码存放在 errno.
2.2 文件操作(读,写)
read
头文件: #include <unistd.h>
#include <fcntl.h>
函数原型: ssize_t read(int fd,void* buf,size_t count);
函数功能: 读取文件内容
函数参数:fd : 目标文件的文件描述符
buf : 已申请的内存空间首地址,用于存储读取到的文件内容
count: 欲读取的字节数.
返回值: 成功: 返回实际读取到的字节数
0: 代表读取到文件末尾
失败: 返回-1:错误码存放在 errno.write
头文件: #include <unistd.h>
#include <fcntl.h>
函数原型: ssize_t write(int fd,const void* buf,size_t count);
函数功能: 写入文件内容
函数参数: fd : 目标文件的文件描述符
buf : 已申请的内存空间首地址,用于存储待写入的文件内容
count: 欲读取的字节数.
返回值: 成功: 返回实际写入的字节数
失败: 返回-1:错误码存放在 errno.
2.3 关闭文件,释放文件描述符
close
头文件: #include <unistd.h>
#include <fcntl.h>函数原型: int close(int fd);
函数功能: 关闭文件
函数参数: fd : 目标文件的文件描述符
返回值: 成功: 0
失败: 返回-1:错误码存放在 errno.
3.文件的其他操作
3.1文件的随机读写:
lseek
头文件: #include <unistd.h>
#include <fcntl.h>
函数原型: off_t lseek(int fd,off_t offset,int whence);
函数功能: 移动文件位置指针
函数参数:fd : 目标文件的文件描述符
offset: 偏移量
whence: 参考位置:
SEEK_SET 文件头部
SEEK_CUR 当前位置
SEEK_END 文件末尾
返回值:成功: 返回位置指针当前位置(文件头部)
失败: 返回-1:错误码存放在 errno.
3.2 文件内存映射:
mmap/munmap
头文件: #include <unistd.h>
#include <mman.h>
函数原型: void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
int munmap(void *addr, size_t length);
函数功能: 映射/解除映射 文件存储空间到进程的虚拟地址空间
函数参数:
addr: 映射的地址空间首地址,NULL 表示让系统决定;
length: 地址空间大小
rot: 映射的地址空间访问方式,必须和文件打开方式匹配,常用取值如下:
PROT_READ
PROT_WRITE
flags: 映射的地址空间的访问标记,主要取值如下
MAP_SHARED
MAP_PRIVATR
fd: 需映射的文件描述符
offset: 文件存储空间的偏移量
返回值: 成功: munmap返回 0 mmap返回实际映射的地址
失败: mmap返回 MAP_FAILED,并将错误码存入 errno 中
3.3 设备控制:
ioctl
头文件: #include <unistd.h>
#include <sys/ioctl.h>
函数原型: int ioctl(int fd, unsigned long request, ...);
函数功能: 操作底层设备的设备参数
函数参数:
fd: 设备的描述符;
request: 操作请求码(取决于具体设备)
...: 第三个参数往往取决于 request 参数;
返回值: 成功: 返回 0 部分设备的请求码会返回具体的数据
失败: 返回 -1 并将错误码存入 errno 中
3.4 文件状态信息获取:
stat/fstat
头文件: #include <unistd.h>
#include <sys/ioctl.h>
函数原型: int stat(const char* filepath, struct stat *status);
int fstat(int fd, struct stat *status);
函数功能: 获取文件状态信息
函数参数:
fd: 目标文件的描述符;
filepath: 目标文件路径
status: 指向struct stat的指针,用于存储获取到的信息数据;
返回值: 成功: 返回 0
失败: 返回 -1 并将错误码存入 errno 中
延伸知识: 时间管理
头文件: #include <time.h>
涉及的类型: time_t struct tm
涉及的接口函数:
1. time_t time(time_t *t); // 获取系统时间
返回自 1970-01-01 00:00:00 到当前所经过的秒数time_t t; time(&t);
time_t t = time(NULL);
2. char* asctime(struct tm * t);
char* ctime(const time_t* t);3. struct tm* localtime(const time_t* t);
time_t mktime(struct tm* t);4. strftime
5. difftime