【Linux之IO系统编程学习】02.write函数和read函数

news/2025/3/17 23:22:17/

 
【Linux之IO系统编程学习】

项目代码获取:https://gitee.com/chenshao777/linux_-io.git
(麻烦点个免费的Star哦,您的Star就是我的写作动力!)

02.write函数和read函数

目录
       一、write函数(man手册)
       二、read函数(man手册)
       三、写入+读取实验
       四、lseek函数
       五、写入+读取实验(加入读写指针移动)

一、write函数(man手册)

man 2 write

得到 man 手册内容

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
其他内容省略......

可以看到需要包含 unistd.h 头文件,且write函数有三个参数,以及一个返回值

项目含义
fd操作的文件描述符
buf写入的数组
count写入的字节数
返回值写入成功的字节数

二、read函数(man手册)

man 2 read

得到 man 手册内容

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
其他内容省略......

可以看到需要包含 unistd.h 头文件,且read函数有三个参数,以及一个返回值

参数含义
fd操作的文件描述符
buf读取的数据存放到该数组
count要读取的字节数
返回值实际读取到的字节数

三、写入+读取实验

使用write函数向文件a.txt中写入数据,使用read函数读取出数据并打印显示

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>int main(int argc, char *argv[])
{int fd;int wr_res, re_res;char wr_buf[100];char re_buf[100];/*通过main参数接收要写入的内容*/sprintf(wr_buf, "%s", argv[2]);/*以清空全部内容和读写方式打开文件*/fd = open(argv[1], O_TRUNC | O_RDWR);if(fd < 0){printf("open file failed , fd = %d\n",fd);return 0;}else{printf("open file success , fd = %d\n",fd);}/*向文件中写入数据*/wr_res = write(fd, wr_buf, strlen(wr_buf));/*write函数的返回值是写入成功的字节数*/printf("write success bytes = %d\n",wr_res)/*读取文件内容*/re_res = read(fd, re_buf, sizeof(re_buf));/*read函数的返回值是读取成功的字节数*/printf("read success byte = %d\n",re_res);printf("read data = %s\n", re_buf);close(fd);return 0;
}

步骤:
1.通过main参数接收要写入的内容
2.以清空全部内容和读写方式打开文件
3.使用write函数向文件中写入数据
4.使用read函数读取文件内容,并打印读取成功的字节数与读取的内容

实验结果:

hc@hc-vm:~/Linux_ARM/git/linux_-io/02.write和read函数$ ./a.out a.txt nihao
open file success , fd = 3
write success bytes = 5
read success byte = 0
read data = 

发现读取成功的字节数是0,这就要引入一个概念,就是文件读写指针,由于我们是先写入内容,所以文件读写指针在文件的最后位置,所以read的时候默认从最后开始读取,所以读不到数据


四、lseek函数

使用lseek函数可以操作文件读写指针的位置
使用man手册查看

man 2 lseek
省略其他内容......
SYNOPSIS#include <sys/types.h>#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);DESCRIPTIONlseek() repositions the file offset of the open file description associated with the file descriptorfd to the argument offset according to the directive whence as follows:SEEK_SETThe file offset is set to offset bytes.SEEK_CURThe file offset is set to its current location plus offset bytes.SEEK_ENDThe file offset is set to the size of the file plus offset bytes.
省略其他内容......
off_t lseek(int fd, off_t offset, int whence);
参数含义
fd操作的文件描述符
offset偏移量(相对于whence参数)
whence当前位置基点
返回值文件读写指针当前位置(失败返回-1)

whence参数有三个选项
SEEK_SET : 文件开头位置
SEEK_CUR: 当前读写指针位置
SEEK_END: 文件结尾位置
所以在读取文件之前,先将读写指针调到文件开头位置即可读取成功
即加入下面这一句

lseek(fd, 0, SEEK_SET);

五、写入+读取实验(加入读写指针移动)

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>int main(int argc, char *argv[])
{int fd;int wr_res, re_res;char wr_buf[100];char re_buf[100];/*通过main参数接收要写入的内容*/sprintf(wr_buf, "%s", argv[2]);/*以清空全部内容和读写方式打开文件*/fd = open(argv[1], O_TRUNC | O_RDWR);if(fd < 0){printf("open file failed , fd = %d\n",fd);return 0;}else{printf("open file success , fd = %d\n",fd);}/*向文件中写入数据*/wr_res = write(fd, wr_buf, strlen(wr_buf));/*write函数的返回值是写入成功的字节数*/printf("write success bytes = %d\n",wr_res);/*调整文件定位指针位置, 相对文件开头位置偏移0个位置*/lseek(fd, 0, SEEK_SET);/*读取文件内容*/re_res = read(fd, re_buf, sizeof(re_buf));/*read函数的返回值是读取成功的字节数*/printf("read success byte = %d\n",re_res);printf("read data = %s\n", re_buf);close(fd);return 0;
}

运行结果:

hc@hc-vm:~/Linux_ARM/git/linux_-io/02.write和read函数$ ./a.out a.txt nihao
open file success , fd = 3
write success bytes = 5
read success byte = 5
read data = nihao

读写成功!


http://www.ppmy.cn/news/74513.html

相关文章

uni-app小程序uni.navigateBack返回上一个页面并传递参数.返回上个页面并刷新

返回上一个打开的页面并传递一个参数。有种办法就是使用 假如从B页面返回A页面&#xff1a; var pages getCurrentPages(); var prevPage pages[pages.length - 2]; //上一个页面 prevPage.setData({ mdata:1 })经过测试&#xff0c;在uni.app中使用B页面使用setData设置A页…

ffmpeg编译链接错误

编译脚本参见 官方文档 https://trac.ffmpeg.org/wiki/CompilationGuide/Centos 编译链接错误 ● error: undefined reference to pthread_once ,undefined reference to uncompress错误 ○ pthread并将它链接到程序 加上- pthread ; ● error: undefined reference to un…

Redis--弱口令未授权访问漏洞

Redis--弱口令未授权访问漏洞 一、漏洞简介二、危险等级三、漏洞影响四、入侵事件五、漏洞复现--Redis CrackIT入侵事件5.1、以root启动的redis&#xff0c;可以远程登入到redis console--------A主机5.2、生成公钥5.3、执行: redis-cli flushall 清空redis(非常暴力&#xff0…

什么是FTP服务器?有哪些作用?

FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机&#xff0c;它们依照FTP协议提供服务。FTP协议是File Transfer Protocol(文件传输协议)。顾名思义&#xff0c;就是专门用来传输文件的协议。简单地说&#xff0c;支持FTP协议的服务器就是…

HTML <button> 标签

实例 以下代码标记了一个按钮: <button type="button">Click Me!</button> 浏览器支持 元素ChromeIEFirefoxSafariOpera<button>YesYesYesYesYes所有主流浏览器都支持 <button> 标签。 重要事项:如果在 HTML 表单中使用 button 元素,不…

Qt编程基础 | 第三章-控件 | 3.1、组合框

一、组合框 1.1、定义 QComboBox提供了一种向用户呈现选项列表的方式&#xff0c;以占用最少的屏幕空间。 组合框是一个显示当前项目的选择小部件&#xff0c;可以弹出可选择项目的列表。 组合框可以是可编辑的&#xff0c;允许用户修改列表中的每个项目。 QComboBox 除了显示…

用iOS版ChatGPT第一步:手把手带你注册美区Apple ID!(史上最简单)

大家好&#xff0c;我是鸟哥。 前两天ChatGPT官方毫无征兆的上线了iOS版&#xff0c;和网页版的相比功能和响应速度都提升了N个档次&#xff0c;具体看这篇文章&#xff1a;iOS版ChatGPT突然上线&#xff01;Plus用户笑疯了&#xff01; 但是呢&#xff0c;目前iOS版只在美区…

【数据结构】--单链表力扣面试题③找链表的中间节点

目录 法一&#xff1a;遍历链表法 法二、快慢指针法 题述&#xff1a;给定一个头结点为head的非空单链表&#xff0c;返回链表的中间节点。如果有两个中间节点&#xff0c;则返回第二个中间节点。 示例1&#xff1a; 输入&#xff1a;【1,2,3,4,5】 输出&#xff1a;此链表…