2025年Linux下系统函数open,read,write,lseek函数

Linux下系统函数open,read,write,lseek函数一 简介 1 open 头文件 include sys types h include sys stat h include fcntl h 定义函数 int open const char pathname int flags int open const char pathname fcntl h sys sys

大家好,我是讯享网,很高兴认识大家。

一、简介

1. open
  • 头文件

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>

  • 定义函数:
  • 函数说明:
    第一个参数pathname 指向欲打开的文件路径字符串。
    第二参数flags 所能使用的旗标:
    1.O_RDONLY 只读打开。
    2.O_WRONLY 只写打开。
    3.O_RDWR 读、写打开。
    4.O_APPEND 每次写时都加到文件的尾端。
    5.O_CREAT 若此文件不存在则创建它。使用此选择项时,需同时说明第
    第三个参数mode,用其说明该新文件的存取许可权位。
    参数mode 则有下列数种组合, 只有在建立新文件时才会生效, 此外真正建文件时的权限会受到umask 值所影响, 因此该文件权限应该为 (mode-umaks).
    S_IRWXU00700 权限, 代表该文件所有者具有可读、可写及可执行的权限.
    S_IRUSR 或S_IREAD, 00400 权限, 代表该文件所有者具有可读取的权限.
    S_IWUSR 或S_IWRITE, 00200 权限, 代表该文件所有者具有可写入的权限.
    S_IXUSR 或S_IEXEC, 00100 权限, 代表该文件所有者具有可执行的权限.
    S_IRWXG 00070 权限, 代表该文件用户组具有可读、可写及可执行的权限.
2.Read
  • 定义函数

ssize_t read(int fd, void * buf, size_t count);

  • 使用的头文件:

#include <unistd.h>

  • 函数的参数说明

【int fd】这个是文件指针
【void *buf】读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移
【size_t count】是请求读取的字节数。若参数count 为0, 则read()不会有作用并返回0. 返回值为实际读取到的字节数, 如果返回0

3.Write
  • 定义函数

ssize_t write (int fd, const void * buf, size_t count);


讯享网

  • 函数说明

write()会把参数buf 所指的内存写入count 个字节到参数fd 所指的文件内. 当然, 文件读写位置也会随之移动.

4.sleek
  • 定义函数
    off_t lseek(int fildes,off_t offset,int whence)
  • 函数说明
    每个已打开的文件都有一个读写位置,当打开文件通常的读写位置是指向文件的开头,若是以追加的方式打开(调用open 函数时使用了O_APPEND),则读写位置会指向文件末尾,当调用read()或creat()时,读写的位置随之增加;参数fildes为已打开的文件描述符,参数offset为根据参数whence来移动的读写位置的偏移量.文件指针的值等于当前指针的值加上offset的值。
  • 1.SEEK_SET
    -从文件开始处计算偏移量,文件指针到文件开始处的偏移量为offset.
  • 2.SEEK_CUR
    -从文件的指针的当前位置开始计算偏移量,文件指针值等于当前指针值加上offset的值,offset允许为负数.
  • 3.SEEK_END
    -从文件的结尾处计算偏移量,文件指针值等于当前指针的值加上offset的值.offset 允许取负数

二、实现文件的复制

#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main(int argc, const char* argv[]) { 
    int fd01 = open("english.txt", O_RDWR);//可读可写 printf("fd01=%d\n", fd01); if (fd01 == -1) { 
    perror("Open failed"); //错误代码写入errno,perror根据errno的数值显示相对应的错误 } //打开另一个文件,若没有则建立 int fd02 = open("test04", O_WRONLY | O_CREAT, 0777); printf("fd02=%d\n", fd02); //read读4K char buf[1024 * 2];//缓冲区 int len = read(fd01, buf, sizeof(buf));//sizeof包含\0 while (len > 0)//==0就是读完 { 
    int fd03 = write(fd02, buf, len);//写 printf("fd03=%d", fd03); //文件指针再移动4k,继续读 len = read(fd01, buf, sizeof(buf)); } close(fd01); close(fd02); return 0; } 

讯享网
讯享网gcc system_func.c -o system_func ./system_func vim test04 

结果显示:
在这里插入图片描述

三、文件的拓展(lseek)

#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main(int argc, const char* argv[]) { 
    int fd = open("english.txt", O_RDWR); if (fd == -1) { 
    perror("open error!"); } //文件指针移到最后,再偏移1000 int len = lseek(fd, 1000, SEEK_END); write(fd, "a", 1);//必须写入才生效 close(fd); return 0; } 
讯享网ll english.txt gcc lseekTest.c -o lseekTest ./lseekTest ll english.txt 

结果显示:
在这里插入图片描述
说明:
系统显示全是乱码,其实全部是"\0",占位的作用。
在这里插入图片描述

  • 如下载一个10G电影,刚开始就会在磁盘上先分配10G内存,先占位,怕后面别人占用。
  • 下载是多线程的,如1号指针负责前部分,2号指针负责后部分,占位后2号指针才能找到,才能移动过。
小讯
上一篇 2025-01-09 20:51
下一篇 2025-01-19 17:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/74305.html