要了解linux文件io,必须掌握几个关键概念。首先,我们需要理解系统调用和库函数之间的区别。
系统调用和库函数的概念: 在讨论返回值之前,先介绍两个概念:系统调用和库函数。许多库函数实际上是系统调用的封装,这使得它们更易于二次开发。库函数(libc)包括fopen、fclose、fread、fwrite等,而系统调用接口则包括open、close、read、write、lseek等。
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296713297.jpg)
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296845674.jpg)
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296887202.jpg)
区分内核文件结构体和C标准库中的文件结构体:
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296839244.jpg)
硬件层面与内核的交互: 硬件的读写方式各有不同,通过特定的接口和机制(如遵循特定的声卡驱动架构)与系统I/O进行交互。在与I/O口进行交互的过程中,少不了与虚拟文件系统的交互,相关的部分也涉及子类继承基类来实现IO传输功能。
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296872511.jpg)
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296912421.jpg)
【库函数】
回顾C语言中读写文件的方式,库函数包括fopen、fclose、fread、fwrite等。我们观察到打开文件的方式有r、r+、w、w+等选项。此外,C语言还有printf、scanf、fwrite、fprintf、fseek、ftell、rewind等读写方式,这些底层都是系统接口的封装。
FILE *fp = fopen("myfile", "w"); // 写
FILE *fp = fopen("myfile", "r"); // 读打开文件的方式:
r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate(缩短) file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
【系统调用接口】
系统接口进行文件IO访问,系统调用接口包括open、close、read、write、lseek。
【1】系统调用:open接口介绍与使用演示
![【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)](https://img.php.cn/upload/article/001/503/042/175280296950659.jpg)
查看手册:man open
头文件:#include <sys>
#include <sys>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。
参数:
O_RDONLY: 只读打开
O_WRONLY: 只写打开
O_RDWR: 读,写打开
这三个常量,必须指定一个且只能指定一个
O_CREAT: 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限
O_APPEND: 追加写
O_TRUNC: 先清空文件内容
返回值:
成功:新打开的文件描述符
失败:-1
mode_t:
权限设置
</fcntl.h></sys></sys>//按照写方式的打开,文件不存在就创建,但会先清空文件内容
int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
//按照写方式打开,文件不存在就创建,从文件结尾开始写入(追加,不先清空文件内容)
int fd = open("loga.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
close(fd);【2】系统调用:read接口介绍与使用演示
头文件:#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
参数:
fd:文件描述符,是一个非负整数,用于标识要写入数据的文件。
buf:写入数据的缓冲区的首地址
count:要写入的数据的字节数。
返回值:
成功时,返回实际写入的字节数。(这个值可能小于请求的字节数,但绝不会大于请求的字节数)
失败时,返回-1,并设置errno以指示错误类型。
</unistd.h>//打开文件,只写
int fd = open("example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
//参数准备
int count = 5;
const char *msg = "hello bit!\n";
int len = strlen(msg);
//使用
while(count--) {
write(fd, msg, len);
//fd: 文件描述符, msg:缓冲区首地址, len: 本次读取,期望写入多少个字节的数据。 返回值:实际写了多少字节数据
}【3】系统调用:write接口介绍与使用演示
头文件
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
参数:
fd:文件描述符,是一个非负整数,用于标识要读取数据的文件。
buf:指向用户空间中用于存储读取数据的缓冲区的指针。
count:要读取的数据的字节数。
返回值:
成功时,返回实际读取的字节数。这个值可能小于请求的字节数,表示已到达文件末尾或发生了其他读取限制。
失败时,返回-1,并设置errno以指示错误类型。
</unistd.h>//打开文件,只读
int fd = open("example.txt", O_RDONLY);
//参数准备
char buffer[1024];
ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
buffer[bytes_read] = '\0'; // 确保缓冲区以空字符结尾,用于字符串处理
close(fd);以上就是【Linux】文件IO系统[ 库函数 ]封装了[ 系统调用 ] +【区分文件结构体FILE和file与files_srtuct表】(读写接口盘点与介绍)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号