【Linux系统编程(文件编程)】之读、写文件、文件光标移动

一、文件写入

在这里插入图片描述
write() writes up to count bytes from the buffer starting at buf to the file referred to by the file descriptor fd.write()
write() 函数,将从buf缓冲区开始,写count个字节,写入到文件描述符fd引用的文件

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main()
{
        int fd;
        char *buf = "changxianrui hen shuai !!!";
        fd = open("./file", O_RDWR);
        if(fd == -1){
                printf("open file failed\n");
                fd = open("./file", O_RDWR|O_CREAT, 0600);
                if(fd > 0)
                {
                        printf("create file success");
                }
        }
        printf("open success: fd=%d\n", fd);
//      ssize_t write(int fd, const void *buf, size_t count);
        write(fd, buf, strlen(buf));

        close(fd);
        return 0;
}

在这里插入图片描述
strlen()函数用来统计字符串的长度,遇到\0结束统计,即函数返回值不包括\0。例如:strlen("abc")的返回值为3,但是sizeof("abc")的返回值是4,这里要留意。

二、文件读取

如果读取不到内容可能是光标的问题 重新打开或者移动光标

在这里插入图片描述
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
read() 从文件描述符fd,读取count个字节的数据,存到buf缓冲区。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

int main()
{
        int fd;
        char *buf = "changxianrui hen shuai !!!";
        fd = open("./file", O_RDWR);
        if(fd == -1){
                printf("open file failed\n");
                fd = open("./file", O_RDWR|O_CREAT, 0600);
                if(fd > 0)
                {
                        printf("create file success\n");
                }
        }
        printf("open success: fd=%d\n", fd);
//      ssize_t write(int fd, const void *buf, size_t count);
        int n_write = write(fd, buf, strlen(buf));
        if(n_write != -1){
                printf("write %d bytes to file\n", n_write);
        }


        close(fd);


        fd = open("./file", O_RDWR);
        char *readBuf;
        readBuf = malloc(sizeof(char) * n_write + 1);  // 没有这步段错误
//      ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd, readBuf, n_write);
        printf("read %d bytes, context:%s\n", n_read, readBuf);
        close(fd);
        return 0;
}

三、文件光标移动

lseek()函数的作用:让文件指针相对whence参数移动offest字节,offest为正数则向后移动,反之负数向前移。

文件指针是你在这个文件中所处的位置,进行读写操作的时候,都是从文件指针所在的位置开始。

在这里插入图片描述
lseek()函数的返回值是距离文件头的字节数。

可以利用移动光标读文件。因为先写文件,后读文件,移动光标避免读不到。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

int main()
{
        int fd;
        char *buf = "changxianrui hen shuai !!!";
        fd = open("./file", O_RDWR);
        if(fd == -1){
                printf("open file failed\n");
                fd = open("./file", O_RDWR|O_CREAT, 0600);
                if(fd > 0)
                {
                        printf("create file success\n");
                }
        }
        printf("open success: fd=%d\n", fd);
//      ssize_t write(int fd, const void *buf, size_t count);
        int n_write = write(fd, buf, strlen(buf));
        if(n_write != -1){
                printf("write %d bytes to file\n", n_write);
        }


        char *readBuf;
        readBuf = malloc(sizeof(char) * n_write + 1);  // 没有这步段错误
//      ssize_t read(int fd, void *buf, size_t count);
//      off_t lseek(int fd, off_t offset, int whence);
        //lseek(fd, 0, SEEK_SET);
        lseek(fd, -26, SEEK_END);//正数向后移动,负数向后移动
        int n_read = read(fd, readBuf, n_write);
        printf("read %d bytes, context:%s\n", n_read, readBuf);
        close(fd);
        return 0;
}

使用 lseek() 计算文件大小

在这里插入图片描述

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

int main()
{
        int fd;
        fd = open("./file", O_RDWR);
        int size = lseek(fd, 0, SEEK_END);//正数向后移动
        printf("read %d bytes\n", size);
        close(fd);
        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值