《UNIX环境高级编程》笔记--read函数,write函数,lseek函数

2024-02-21 09:38

本文主要是介绍《UNIX环境高级编程》笔记--read函数,write函数,lseek函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.read函数

调用read函数从文件去读数据,函数定义如下:
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <unistd.h>   
  2. ssize_t read(int filedes, void* buff, size_t nbytes);  
#include <unistd.h>
ssize_t read(int filedes, void* buff, size_t nbytes);
成功则返回实际读取的byte数,如果已经达到文件结尾则返回0,出错则返回-1.

2.write函数

调用write函数向打开的文件写入数据,函数定义如下:
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <unistd.h>   
  2. ssize_t write(int filedes, void* buff, size_t nbytes);  
#include <unistd.h>
ssize_t write(int filedes, void* buff, size_t nbytes);
成功则返回实际写入的byte数,出错则返回-1.

read和write的buff大小为一个文件block大小时,效率是最高的,在ext4文件系统中,一个文件block大小为4K,一般这个块长存放
在stat结构中,定义如下:
blksize_t st_blksize;
可以使用stat系列函数获取该值。

注意:
使用read,write操作管道,FIFO以及某些设备时,特别是终端,网络和STREAMS,有下列两种性质。
a.一次read操作所返回的数据可能少于所要求的数据,即使还没达到文件尾端也可能是这样。这不是一个错误,应该
继续读该设备。
b.一次write操作的返回值也可能少于指定输出的字节数。这也不是错误,应当继续写余下的数据至该设备。

3.lseek函数

每个打开的文件都有一个关联的“当前偏移量”,用于记录从文件到当前当前位置的偏移字节数,lseek函数是设置这个当前偏移量

的函数,函数的声明如下:
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <unistd.h>   
  2. off_t lseek(int filedes, off_t offset, int whence);  
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
成功则返回新的文件偏移量,失败则返回-1.

如果whence是SEEK_SET,则文件的偏移量设置为文件开始加上offset个字节。
如果whence是SEEK_CUR,则文件的偏移量设置为当前偏移量开始加上offset个字节,offset可正可负。
如果whence是SEEK_END,则文件的偏移量设置为文件长度加上offset个字节,offset可正可负。

不是每个文件都能够设置偏移量,有些文件如管道,FIFO或socket,无法设置偏移量,可以使用如下函数测试是否可以设置偏移量,
如果返回-1,则表示不可以。
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. off_t currpos;  
  2. currpos = lseek(fd, 0, SEEK_CUR);  
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);

创建一个文件,往文件中写入10个字符,然后再使用lseek定位文件开始加上4个字节的偏移量,然后读取接下来的内容。
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <fcntl.h>   
  2. #include <stdio.h>   
  3.   
  4. int main(void){  
  5.         int fd,byteNum,result;  
  6.         char wbuf[10] = "123456789";  
  7.         char rbuf[10];  
  8.         if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){  
  9.                 perror("open");  
  10.                 return -1;  
  11.         }  
  12.   
  13.         if((byteNum = write(fd, wbuf, 10))<0){  
  14.                 perror("write");  
  15.                 return -1;  
  16.         }  
  17.   
  18.         if((result = lseek(fd, 4, SEEK_SET))<0){  
  19.                 perror("lseek");  
  20.                 return -1;  
  21.         }  
  22.   
  23.         if((byteNum = read(fd, rbuf, 10)) < 0){  
  24.                 perror("read");  
  25.                 return -1;  
  26.         }  
  27.   
  28.         printf("read content:%s\n",rbuf);  
  29.   
  30.         close(fd);  
  31.         return 0;  
  32. }  
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, 4, SEEK_SET))<0){
perror("lseek");
return -1;
}
if((byteNum = read(fd, rbuf, 10)) < 0){
perror("read");
return -1;
}
printf("read content:%s\n",rbuf);
close(fd);
return 0;
}
运行结果:
read content:56789

如果将偏移量设置为文件开始加上一个负数,调用lseek就会出错。
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <fcntl.h>   
  2. #include <stdio.h>   
  3.   
  4. int main(void){  
  5.         int fd,byteNum,result;  
  6.         char wbuf[10] = "123456789";  
  7.         char rbuf[10];  
  8.         if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){  
  9.                 perror("open");  
  10.                 return -1;  
  11.         }  
  12.   
  13.         if((byteNum = write(fd, wbuf, 10))<0){  
  14.                 perror("write");  
  15.                 return -1;  
  16.         }  
  17.   
  18.         if((result = lseek(fd, -1, SEEK_SET))<0){  
  19.                 perror("lseek");  
  20.                 return -1;  
  21.         }  
  22.   
  23.         if((byteNum = read(fd, rbuf, 10)) < 0){  
  24.                 perror("read");  
  25.                 return -1;  
  26.         }  
  27.   
  28.         printf("read content:%s\n",rbuf);  
  29.   
  30.         close(fd);  
  31.         return 0;  
  32. }  
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, -1, SEEK_SET))<0){
perror("lseek");
return -1;
}
if((byteNum = read(fd, rbuf, 10)) < 0){
perror("read");
return -1;
}
printf("read content:%s\n",rbuf);
close(fd);
return 0;
}
运行结果:
lseek: Invalid argument

是不是offset就一定不能为负数呢,不一定,只要最终的偏移量不小于0就可以了,看下面的例子。
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <fcntl.h>   
  2. #include <stdio.h>   
  3.   
  4. int main(void){  
  5.         int fd,byteNum,result;  
  6.         char wbuf[10] = "123456789";  
  7.         char rbuf[10];  
  8.         if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){  
  9.                 perror("open");  
  10.                 return -1;  
  11.         }  
  12.   
  13.         if((byteNum = write(fd, wbuf, 10))<0){  
  14.                 perror("write");  
  15.                 return -1;  
  16.         }  
  17.   
  18.         if((result = lseek(fd, -4, SEEK_CUR))<0){  
  19.                 perror("lseek");  
  20.                 return -1;  
  21.         }  
  22.   
  23.         if((byteNum = read(fd, rbuf, 10)) < 0){  
  24.                 perror("read");  
  25.                 return -1;  
  26.         }  
  27.   
  28.         printf("read content:%s\n",rbuf);  
  29.   
  30.         close(fd);  
  31.         return 0;  
  32. }  
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, -4, SEEK_CUR))<0){
perror("lseek");
return -1;
}
if((byteNum = read(fd, rbuf, 10)) < 0){
perror("read");
return -1;
}
printf("read content:%s\n",rbuf);
close(fd);
return 0;
}
运行结果:
read content:789

如果文件偏移量大于文件长度再写入数据,那么生成的文件就会出现空洞。先往文件写入10个字符,再跳过40960个字符,再写入10个字符。
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <fcntl.h>   
  2. #include <stdio.h>   
  3.   
  4. int main(void){  
  5.         int fd,byteNum,result;  
  6.         char wbuf[10] = "123456789";  
  7.         char rbuf[10];  
  8.         if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))<0){  
  9.                 perror("open");  
  10.                 return -1;  
  11.         }  
  12.   
  13.         if((byteNum = write(fd, wbuf, 10))<0){  
  14.                 perror("write");  
  15.                 return -1;  
  16.         }  
  17.   
  18.         if((result = lseek(fd, 40960, SEEK_END))<0){  
  19.                 perror("lseek");  
  20.                 return -1;  
  21.         }  
  22.   
  23.         if((byteNum = write(fd, wbuf, 10)) < 0){  
  24.                 perror("write");  
  25.                 return -1;  
  26.         }  
  27.   
  28.         close(fd);  
  29.         return 0;  
  30. }  
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, 40960, SEEK_END))<0){
perror("lseek");
return -1;
}
if((byteNum = write(fd, wbuf, 10)) < 0){
perror("write");
return -1;
}
close(fd);
return 0;
}
运行结果:
-rw------- 1 root root 40980 2013-09-09 15:03 a.txt
使用od命令查看文件内容:
root@virtual-machine:~# od -c a.txt
0000000   1   2   3   4   5   6   7   8   9  \0  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0120000  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0   1   2   3   4   5   6
0120020   7   8   9  \0
0120024
空洞的内容全部置为0

查看下文件占用的磁盘大小:
8 -rw------- 1 root root 40980 2013-09-09 15:41 a.txt
文件虽然有40980个字节,只占用了8个block,即8K(这边的block不是文件系统的block,而是kernel block,linux中,
kernel block大小为1K),如果是没有空洞的文件,则占用磁盘大小应该是44K。可见,虽然有空洞,但是实际存储时没
有占用文件大小的磁盘空间。

如果文件以O_APPEND方式打开,但是在写入前lseek到文件的某一个位置,结果会怎样?
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>   
  2. #include <fcntl.h>   
  3.   
  4. int main(void){  
  5.         int fd,result;  
  6.         char wbuf[5] = "1234";  
  7.         if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){  
  8.                 perror("open");  
  9.                 return -1;  
  10.         }  
  11.   
  12.         if((result = lseek(fd, 2, SEEK_SET)) < 0){  
  13.                 perror("lseek");  
  14.                 return -1;  
  15.         }  
  16.   
  17.         if((result = write(fd, wbuf, 4))<0){  
  18.                 perror("write");  
  19.                 return -1;  
  20.         }  
  21.   
  22.         close(fd);  
  23.         return 0;  
  24. }  
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd,result;
char wbuf[5] = "1234";
if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){
perror("open");
return -1;
}
if((result = lseek(fd, 2, SEEK_SET)) < 0){
perror("lseek");
return -1;
}
if((result = write(fd, wbuf, 4))<0){
perror("write");
return -1;
}
close(fd);
return 0;
}
程序执行前a.txt为:123456789
程序执行后a.txt为:
123456789
1234
因为设置为O_APPEND后,内核每次对这种文件写之前,进程当前的偏移量都会设置到文件的末尾。而且lseek和write
合起来是一个原子操作。

在使用lseek函数的时候发现一个问题,如果调用如下:lseek(fd, -2, SEEK_END),则还是在文件的末尾写入,不知道
为什么。
[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>   
  2. #include <fcntl.h>   
  3.   
  4. int main(void){  
  5.         int fd,result;  
  6.         char wbuf[5] = "1234";  
  7.         if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){  
  8.                 perror("open");  
  9.                 return -1;  
  10.         }  
  11.   
  12.         if((result = lseek(fd, -2, SEEK_END)) < 0){  
  13.                 perror("lseek");  
  14.                 return -1;  
  15.         }  
  16.   
  17.         if((result = write(fd, wbuf, 4))<0){  
  18.                 perror("write");  
  19.                 return -1;  
  20.         }  
  21.   
  22.         close(fd);  
  23.         return 0;  
  24. }  
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd,result;
char wbuf[5] = "1234";
if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){
perror("open");
return -1;
}
if((result = lseek(fd, -2, SEEK_END)) < 0){
perror("lseek");
return -1;
}
if((result = write(fd, wbuf, 4))<0){
perror("write");
return -1;
}
close(fd);
return 0;
}
运行结果:
yan@yan-vm:~/ctest$ od -c a.txt
0000000   1   2   3  \n
0000004
yan@yan-vm:~/ctest$ ./a.out
yan@yan-vm:~/ctest$ od -c a.txt
0000000   1   2   3  \n   1   2   3   4
0000010

这篇关于《UNIX环境高级编程》笔记--read函数,write函数,lseek函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/731344

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

SQLite3 在嵌入式C环境中存储音频/视频文件的最优方案

《SQLite3在嵌入式C环境中存储音频/视频文件的最优方案》本文探讨了SQLite3在嵌入式C环境中存储音视频文件的优化方案,推荐采用文件路径存储结合元数据管理,兼顾效率与资源限制,小文件可使用B... 目录SQLite3 在嵌入式C环境中存储音频/视频文件的专业方案一、存储策略选择1. 直接存储 vs

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java中的for循环高级用法

《Java中的for循环高级用法》本文系统解析Java中传统、增强型for循环、StreamAPI及并行流的实现原理与性能差异,并通过大量代码示例展示实际开发中的最佳实践,感兴趣的朋友一起看看吧... 目录前言一、基础篇:传统for循环1.1 标准语法结构1.2 典型应用场景二、进阶篇:增强型for循环2.