当前位置:网站首页>文件IO(1)
文件IO(1)
2022-06-23 09:48:00 【蜗牛也执着】
(1)文件IO:
- 打开文件
函数:open
头文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
函数原型:
int open(const char *pathname, int flags); ------》适合以只读的方式打开文件
功能:用来打开一个文件
参数:
参数1:打开的文件名(可以包含路径)
参数2:打开文件的方式
方式有三种:主标志(互斥的)
O_RDONLY
O_WRONLY
O_RDWR
副标志:O_CREAT O_TRUNC
返回值:
成功返回一个文件描述符(非负的整数)
失败则返回-1(errno is set)
int open(const char *pathname, int flags, mode_t mode);---》适合以只写的方式打开文件
功能:用来打开一个文件
参数:
参数1:打开的文件名(可以包含路径)
参数2:打开文件的方式
方式有三种:主标志(互斥的)
O_RDONLY
O_WRONLY
O_RDWR
副标志:O_CREAT O_TRUNC
参数3:权限 -----》可以用八进制表示
返回值:
成功返回一个文件描述符(非负的整数)
失败则返回-1(errno is set)

- 操作文件
2-1写文件:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
功能:
用来给指定的被打开的文件写入数据
参数:
参数1:打开文件成功之后的文件描述符
参数2:存储即将被写入的数据的一个缓冲区
参数3:需要写入的数据元素的个数
返回值:
成功返回成功写入数据的个数
失败则返回-1(已经设置好了errno的值)
2-2读文件:
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
功能:
从指定的被打开的文件读入数据
参数:
参数1:打开文件成功之后的文件描述符
参数2:存储读取到的内容的一个缓冲区
参数3:需要读取的数据元素的个数
返回值:
成功返回成功读取数据的个数
失败则返回-1(已经设置好了errno的值)
返回值等于0,代表读完了
- 关闭文件
#include <unistd.h>
int close(int fd);
参数:打开文件成功之后的文件苗描述符
案例1:实现四个函数搭配使用

案例2:利用文件IO实现CP命令

- 定位
函数名称:lseek
头文件:
#include <sys/types.h>
#include <unistd.h>
函数原型:
off_t lseek(int fd, off_t offset, int whence);
功能:实现跳转
参数:
参数1:打开文件成功之后的文件描述符
参数2:偏移量
参数3:相对于偏移量的基点位置
SEEK_SET 文件开头位置
SEEK_CUR 文件当前位置
SEEK_END 文件末尾位置
返回值:成功则返回文件当前的位移。
失败则返回-1;

案例:验证lseek的返回值是什么的问题?

总结:lseek的返回值代表当前成功偏移的字节数!
- 空洞文件
5-1.什么是空洞文件?

5-2.如何创建一个空洞文件?

代码实现过程如下:(文件IO实现的),也可以用标准IO实现

- 目录相关
- 打开目录
头文件:
#include <sys/types.h>
#include <dirent.h>
函数原型:
DIR *opendir(const char *name);
功能:打开一个目录
参数:
需要打开的目录的名称(可以带路径)
返回值:
成功则返回一个目录指针
失败则返回NULL
- 读目录
#include <dirent.h>
函数原型:
struct dirent *readdir(DIR *dirp);
功能:用来读取目录的内容
参数:打开目录成功的返回值
返回值:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
案例1:实现ls -a命令
案例2:实现ls命令
学习如何测试文件属性?
函数:stat/lstat/fstat
头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
函数原型:
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
注意:以上三个函数均可以测试文件的属性。
其中:
对于fstat而言:该函数第一个参数是一个已经被打开的文件的文件描述符,我们一般不采取这种方式。
对于stat和lstat而言:这两的参数是一样的,但是其本质稍微不同。
stat:称为追踪函数或者穿透函数,意思是如果使用stat函数测试文件属性时,传入的第一个参数是一个软链接文件,那么意味着该函数测试的是该连链接文件指向的源文件,不是链接文件自身。
lstat:不是追踪(穿透)函数,意思是当传入的第一个参数是链接文件时,测试属性还是该链接文件自身的属性。
分析函数:以lstat为例:
int lstat(const char *path, struct stat *buf);
功能:实现可以测试指定的文件的属性
参数:
参数1:需要测试的文件名(可带路径)
参数2:一个存放文件属性的地址信息结构
返回值:成功返回0,失败返回-1
注意:lstat第二个参数的地址信息结构如下:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
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; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated*/
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
- 关闭目录
头文件:
#include <sys/types.h>
#include <dirent.h>
函数原型:
int closedir(DIR *dirp);
功能:关闭打开的目录
参数:opendir的返回值(目录指针)
返回值:成功返回0,失败返回-1
、
边栏推荐
- JSP getting started summary
- 2022 gdevops global agile operation and maintenance summit - essence playback of Guangzhou station (with PPT download)
- [MRCTF2020]Ez_bypass
- Chain implementation of stack -- linear structure
- A 32KB cache with direct mapping Memory exercises after class
- [CISCN2019 华北赛区 Day2 Web1]Hack World
- [SUCTF 2019]CheckIn
- UEFI 学习3.6 - ARM QEMU上的ACPI表
- [MRCTF2020]Ez_ bypass
- [geek challenge 2019] hardsql
猜你喜欢
随机推荐
[wangdingbei 2020 Qinglong formation]areuserialz
XML related interview questions
[ciscn2019 North China Day2 web1]hack world
启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品
Developer, you may have some misunderstandings about cloud computing
Game of life of leetcode topic analysis
Qiming Xingchen Huadian big data quantum security innovation laboratory was unveiled and two black technology products were released
[GXYCTF2019]BabySQli
Fill the pit for repvgg? In fact, it is the repoptimizer open source of repvgg2
ThinkPHP 2. X/3.0 vulnerability recurrence
2022高考季征文获奖名单公布
Gorm advanced query
[网鼎杯 2020 青龙组]AreUSerialz
Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023
High performance computing center nvme / nvme of nvme of overview
Unable to enter the system normally, press F8 to select other items to try to enter
Find minimum in rotated sorted array
我被提拔了,怎么和原来平级的同事相处?
基于mediapipe的手势数字识别
16. system startup process
![[SUCTF 2019]CheckIn](/img/0e/75bb14e7a3e55ddc5126581a663bfb.png)




![[geek Challenge 2019] hardsql](/img/73/ebfb410296b8e950c9ac0cf00adc17.png)



