当前位置: 首页 > news >正文

文件IO(Linux文件IO)

前言

        本文介绍Linux系统下自带的文件IO的函数。

Linux文件IO相关函数

open函数

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
//调用实例
//只写的方式打开当前路径文件demo.c 没有创建 有清空
int fd = open("./demo.c", O_WRONLY|O_TRUNC|O_CREAT, 0666);

        功能:打开文件

        参数1:要打开文件的路径
        参数2:打开文件方式
        参数3:文件权限

O_RDONLYread only 只读
O_WRONLYwrite only 只写
O_RDWR可读可写
O_TRUNC清空
O_APPEND追加
O_CREAT创建

         返回值:非负的文件描述符,没打开一个文件,系统就会在内核空间开辟一块内存。会分配一个非负文件描述符作为每个文件的唯一标识。 成功,int fd 非负的数;失败,-1。

文件权限掩码(umask)

        在Linux中,文件权限掩码(umask)是一个权限掩码,用于限制新建文件的默认权限。目的是提高系统安全性,通过限制新建文件的默认权限,确保用户不会意外赋予其他用户过多的权限。

 

新创建的文件默认权限为 666(rw-rw-rw-)
新创建的目录默认权限为 777(rwxrwxrwx)

0        代表不掩盖(允许设置该权限)
1        代表掩盖执行权限
2        代表掩盖写入权限
3        代表掩盖读取权限 

 

close函数

#include <unistd.h>
int close(int fd);

        功能:关闭文件
        参数:open函数的返回值,打开文件的唯一标识
        返回值:成功,0;失败,-1。 

read函数

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
//调用案例
char buf[128] = {0};    //存储数据
read(fd, buf, sizeof(buf));

        功能:按照字节来读取文件内容

        参数1:open的返回值,打开文件的唯一标识
        参数2:读取内容存放的首地址
        参数3:能读取的最多字节数

        返回值:成功,实际读取的字节数;失败,-1 

write函数

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
//调用案例
char buf[128] = "hello world";
write(fd, buf, strlen(buf));

        功能:按照字节写入内容

        参数1:open的返回值,打开文件的唯一标识
        参数2:要写入内容存放的首地址
        参数3:最多要写入的字节数

        返回值:成功,实际写入的字节数;失败,-1。 

lseek函数

#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
typedef long off_t;
//调用实例
long res = lseek(fd,100,SEEK_SET); //相对文件起始位置向后移动100个字符
lseek(fd,-100,SEEK_END); //相对于文件尾位置向前移动100个字符

        功能:移动文件光标指针的同时,还能获取文件光标指针的位置(兼具fseek和ftell函数的功能)

        参数1:open的返回值,打开文件的唯一标识
        参数2:偏移量,正数向后,负数向前
        参数3:基准位
                SEEK_SET        文件头
                SEEK_END        文件尾
                SEEK_CUR        当前位置 

        返回值:成功,返回文件指针所在位置;失败,-1

统计一个文件有多少个字节的方式(3种)

//1.fgetc一个字符一个字符读 然后计数
while((ch = fgetc(fp))!= EOF)count++;
//2.fseek移动文件指针位置到文件尾 再用ftell获取文件指针位置
fseek(fp,0,SEEK_END);
long size = ftell(fp);
//3.lseek移动文件指针位置到末尾获取文件指针位置即可
long size = lseek(fd,0,SEEK_END);

三个文件描述符

        标准输入:0
        标准输出:1
        标准错误:2

目录操作函数

stat函数

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);struct stat {dev_t     st_dev;         /* ID of device containing file */ino_t     st_ino;         /* Inode number */mode_t    st_mode;        /* File type and mode */nlink_t   st_nlink;       /* Number of hard links */uid_t     st_uid;         /* User ID of owner */gid_t     st_gid;         /* Group ID of owner */dev_t     st_rdev;        /* Device ID (if special file) */off_t     st_size;        /* Total size, in bytes */blksize_t st_blksize;     /* Block size for filesystem I/O */blkcnt_t  st_blocks;      /* Number of 512B blocks allocated *//* Since Linux 2.6, the kernel supports nanosecondprecision for the following timestamp fields.For the details before Linux 2.6, see NOTES. */struct timespec st_atim;  /* Time of last access */struct timespec st_mtim;  /* Time of last modification */struct timespec st_ctim;  /* Time of last status change */#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

opendir函数

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);

readdir函数

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

closedir函数

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);


http://www.mrgr.cn/news/51049.html

相关文章:

  • 二维码生成器 1.02.41| 一站式QR码生成器和美化工具
  • 判断 HTTP/2 多路复用是否在服务器上实现
  • Vue中v-bind对样式控制的增强—(详解v-bind操作class以及操作style属性,附有案例+代码)
  • 10.15学习
  • 【读书笔记-《30天自制操作系统》-28】Day29
  • libaom 源码分析:aomenc.c 文件
  • 【数据结构】二叉树(一)遍历
  • 虚拟机错误:‘VirtualBox Host-Only Ethernet Adapter #2‘
  • 猿人学 — 第1届第4题(解题思路附源码)
  • 前端性能优化之概述篇
  • C/C++语言基础--C++异常看这一篇就够了
  • 关于this指针
  • 海​能​达​一​面
  • 生成一个带有二维数据和对应标签的螺旋形数据集(非线性可分数据集)的代码解析
  • linux线程 | 同步与互斥(上)
  • C语言动态内存开辟
  • 尚硅谷rabbitmq2024 第15-18节 springboot整合与可靠性答疑
  • 在 Spring 中使用 @EhCache 注解作为缓存
  • 如何提高cache miss
  • linux一二三章那些是重点呢