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

 
【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);

DESCRIPTION
       lseek() repositions the file offset of the open file description associated with the file descriptor
       fd to the argument offset according to the directive whence as follows:

       SEEK_SET
              The file offset is set to offset bytes.

       SEEK_CUR
              The file offset is set to its current location plus offset bytes.

       SEEK_END
              The 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

读写成功!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值