代码编织梦想

Linux 系统IO函数

1.open函数

//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//定义
//open 返回文件的描述符,pathname : 需要打开的文件  flags: 以读写或者其他方式打开
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

int creat(const char *pathname, mode_t mode);

int openat(int dirfd, const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);

flags:
O_RDONLY:以只读方式打开文件
O_WRONLY:以只写方式打开文件
O_RDWR:以读写方式打开文件
O_CREAT:如果文件不存在,则创建一个文件,并且需要指定文件的属性(umask)
O_TRUNC:如果文件已经存在,并且文件可写,会把文件截断为0    
O_APPEND:以追加写的方式写文件
ex:
#include <unistd.h>
#include <error.h>
int fd = -1;
fd = open("path", O_RDWR);
if(fd == -1);
printf("fd = %d, error:%d %s", fd, errno, strerror(errno));

2.read函数

//头文件
#include <unistd.h>
//函数原型 从打开的文件描述符fd中读取count大小的内容,返回读取的字节数,把内容存放在buf中,如果返回0,代表读到文件结尾,如果返回-1 最好设置errno变量
ssize_t read(int fd, void *buf, size_t count);

3.write函数

//头文件
#include <unistd.h>
//函数原型  从打开的文件描述符fd文件中写入count大小的内容
ssize_t write(int fd, const void *buf, size_t count);

4.close函数

//头文件
#include <unistd.h>
//函数原型  关闭文件句柄
int close(int fd);

实现拷贝函数

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

int main(int argc, char *argv[])
{
        int fd1 = -1;
        int fd2 = -1;
        char buf[1024] = {0};
        int len = 0;

        if(argc < 2)
                printf("input params fail\n");
        fd1 = open(argv[1], O_RDWR);
        if(fd1 == -1)
                printf("errno:%d fd = %d %s\n", errno, fd1,strerror(errno));
        fd2 = open(argv[2], O_RDWR | O_CREAT, 0644);
        if(fd2 == -1)
                printf("errno:%d fd=%d %s\n", errno, fd2, strerror(errno));

        while(len = read(fd1, buf, sizeof(buf))){

                write(fd2, buf, len);
        }
        close(fd1);
        close(fd2);
        return 0;
}

5.fcntl函数

#include <unistd.h>
#include <fcntl.h>

//对文件描述符进行操作
fd:
cmd:F_GETFD	获取fd信息  arg:void  return: fd的参数
	F_SETFD 设置fd参数	arg:int

int fcntl(int fd, int cmd, ... /* arg */ );

6.lseek函数

#include <sys/types.h>
#include <unistd.h>

fd:需要操作的文件描述符
offset:偏移的大小
whence:位置的位置
SEEK_SET:自定义设置的偏移量
SEEK_CUR:光标设置到当前位置    
SEEK_END:光标设置到文件结束位置   
off_t lseek(int fd, off_t offset, int whence);
//计算文件大小demo
int fd = -1;
int lenth = 0;
fd = open("path", O_RDONLY);
if(fd == -1)
{
	printf("open fail\n",strerror(errno));
	return -1;
}
lenth = lseek(fd, 0, SEEK_END);
printf("lenth:%d", lenth);

7.mmap函数

#include <sys/mman.h>
映射文件或者设备到内存中
/*
addr:映射内存的起始地址,一般为NULL
length:映射文件的大小
prot:映射的内存的读写性		
       PROT_EXEC  Pages may be executed.
       PROT_READ  Pages may be read.
       PROT_WRITE Pages may be written.
       PROT_NONE  Pages may not be accessed.
flags:内存的归属性
		MAP_SHARED:共享内存	MAP_PRIVATE:私有内存
fd:打开文件描述符
offset:偏移量
*/    
void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
int munmap(void *addr, size_t length);
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39678541/article/details/122927282

【linux】系统io和标准c库io函数_julinus的博客-爱代码爱编程

 一、C库IO函数工作流程示意图:                              磁盘为什么这么慢?          大部分硬盘是机械硬盘,读取寻道时间和写入寻道时间都是在毫秒级(ms)、相对于内存来说读写速度都非常快,因为内存术语电子设备,读写速度都是纳秒(ns)级别的。           1s=1000ms

系统io和标准c库io函数 ——linux编程_江山美人1的博客-爱代码爱编程

一、C库IO函数工作流程示意图:                     FILE 类型的指针,是特殊结构体类型,包含文件描述符、读写指针位置、内存地址等信息,用于文件读写操作。 I/O缓冲区用于利用内存减少硬盘操作。在右侧三种情况下刷新缓冲区,存到硬盘上。 磁盘为什么这么慢?          大部分硬盘是机械硬盘,读取寻道时间和写入寻道时间

linux中的io函数_smile_sambery的博客-爱代码爱编程

1)open函数:以特定的方式打开一个文件;                 头文件:sys/type.h  sys/stat.h   fcntl.h                 返回值:错误则返回-1,正确则返回文件描述符(int类型,范围为3~1023,文件的标号)                 函数原型:int open(const  c

linux中的系统io函数_walkingll的博客-爱代码爱编程__io('g', 120)

一. 系统IO函数         1>. 一些概念             文件描述符             PCB             C库函的IO缓冲区         1) 文件描述符              int 类型              一个进程最多可打开多少文件:          2) pcb          

linux系统io常用接口函数-爱代码爱编程

========================================================================     系统io最重要的四个函数,打开open,关闭close,读取read,写入write ===========================================================

linux系统i/o函数_star星屹程序设计的博客-爱代码爱编程

通常,一个进程启动时,默认会打开3个文件:STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO 常用的系统I/O函数: 帮助文档查看方式 (man文档章节2) man 2 open man文档章节2中的open函数 快捷方式:Esc+.(点)  :获取上次命令中所用到的路径 vim编辑器小技巧:光标放在对应函数上,

Linux基础(六) 系统IO函数-爱代码爱编程

使用file命令看文件格式 文章目录 1 open()1.1 打开文件1.2 创建文件1.3 文件权限的计算方式1.4 判断文件已经存在1.5 将文件截断为02 read() write()3 lseek()4 stat()4.1 stat命令4.2 stat函数简介4.3 stat函数使用5 access()6 chmod()7 unlink()

Linux部分系统IO函数-爱代码爱编程

1.系统IO函数: (1)open函数: 1).首先其需要包含头文件:#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> 2).然后就是open的两种参数 int open(const char *pathname,int flags); int op

Linux系统标准IO函数-爱代码爱编程

标准IO函数 使用底层系统调用open/read/write等对文件进行操作效率非常低,原因主要有两个: 使用系统调用会影响系统的性能。与函数调用相比,系统调用的开销较大,因为在执行系统调用时,Linux必须从运行用户代码切换到内核代码,然后再返回用户代码。而减少这种开销的一个好方法是,在程序中尽量减少系统调用的次数,并且让每次系统调用完成尽可能多的工

Linux系统函数之IO函数-爱代码爱编程

技术交流 QQ 群:1027579432,欢迎你的加入! 欢迎关注我的微信公众号:CurryCoder的程序人生 1.标准C库IO函数工作流程 IO缓冲区的作用? 大部分硬盘都是机械硬盘,读取寻道时间和写入寻道时间都是在毫秒级ms;相对来说,内存读写速度都非常块,因为内存属于电子设备,读写速度是纳秒级ns;两者之间的读写速度相差一百万倍;2

Linux基础——系统IO函数-爱代码爱编程

文章目录 1. C库函数1.1 文件指针FILE*概述1.1.1 文件描述符1.1.2 文件读写位置指针1.1.3 I/O缓冲区(一块内存的地址)1.2 虚拟地址空间1.2.1 概述1.2.2 内核区1.2.3 用户区(地址由下至上)1.2.4 问题:为什么有虚拟地址空间?1.3 文件描述符fd1.4 C库函数与系统函数的关系2. 系统I/O函数2

linux调用io函数,系统调用IO和标准IO-爱代码爱编程

1. 系统调用IO(无缓冲IO) 系统调用 在Linux中一切皆文件,文件操作在Linux中是十分重要的。为此, Linux内核提供了一组用户进程与内核进行交互的接口用于对文件和设备进行访问控制,这些接口被称为系统调用。 系统调用对于应用程序最大的作用在于: 以统一的形式,为应用程序提供了一组文件访问的抽象接口,应用程序不需要关心文件的具体类型

Linux系统IO函数-爱代码爱编程

Linux系统IO函数 1. open函数2. close函数3. read函数4. write函数5. lseek函数6. perror函数和全局变量error7. 阻塞与非阻塞参考 1. open函数 函数原型:int open(const char *pathname,int flags); int open(const char *p

Linux 基本I/O库函数-爱代码爱编程

根据《Unix/Linux系统编程》中关于IO库函数部分内容进行总结 包括系统调用与IO库函数关系,基本IO库函数 fopen fclose fgetc getchar getc fputc putchar putc fgets gets fputs puts fscanf scanf sscanf fprintf printf sprintf fread