当前位置:网站首页>Basic IO (on): file management and descriptors
Basic IO (on): file management and descriptors
2022-08-02 04:52:00 【RNGWGzZs】
-----------------"如果说,Do not fight alone.Then let me go"
目录
①File open and file generation location.
问:我们常说,The file is generated in the current executable program directory,这对不对呢?
flagVarious parameters designed according to the bits:
①文件描述符从0开始,并默认0 1 2to third-rate
重定向的本质:Modify the file descriptor pointed toFILE* .
缓冲区:The peripheral files are based on the Von Neumann system,效率低.It's a balanced approach.
The stream is opened by default 与 文件标识符:
(1)复习回顾(C提供文件操作接口):
我们先来回顾在C语言时期,用的C文件接口.
①File open and file generation location.
我们先看看fopen函数:
问:我们常说,The file is generated in the current executable program directory,这对不对呢?
At this point we let the process run.
//Find the process firstpid
ps ajx | grep test
//We know that there is more than one viewing process aboveid的做法
ls /proc/+(进程id)
也就是,cwd和exe目录重叠了.
So we go back one level of directory,
Found that the file generated is no longer in the executable directory.所以,应当是进程运行路径as where the file is generated.
② Write and output
fputs:
Write to text.
The operating system will open three streams for us by default: stdin stdout stderr.
向stdout(显示器)输出:
当然,You must have heard it,linux下一切皆文件.
So how to understand it?
fgets:
从键盘获取
(2)系统调用接口:
不管是C\C++Each has its own set of file calling interfaces.But they all basically call,interface at the system level.
//CLibraries provide interfaces
fopen fclose fwrite fread
//System file calls
open close write read
①open\close:
打开文件的flag有多种方式,modeThe latter refers to the permissions for file creation(default & (~umask))
flag参数 | 说明 |
O_RDONLY | 只读打开 |
O_WRONLY | 只写打开 Does not have the ability to create files |
O_RDWR | 读写打开 |
O_CREAT | 若文件不存在,will create and then open the usual sum(Open the connection),And requires named file permissionsmode |
O_APPEND | 对标fopen("a") |
flagVarious parameters designed according to the bits:
宏判断,多种选项.
②write\read:
(3)文件描述符:
在open中,A successful opening of the file will returnint类型的值,这个就是文件描述符.
①文件描述符从0开始,并默认0 1 2to third-rate
Open several files consecutively.
And print out the file descriptor:
can be seen from the phenomenon,File descriptors are incremented. 但为什么是从3 开始呢?
在前面已经说过,程序运行起来,C会默认打开三个流:
stdin\stdout\stderr,Their file descriptors are respectively0、1、2.
怎么验证呢?
首先我们让printf打印这个 Strings are on the screen.
close(1);
此时我们发现,什么也没打印.
②文件描述符分配规则: 
It was meant to bestdout 打印数据的,But because it's closed,而创建的fd assigned again1
So it is written directly to the file. 这也叫做输出重定向.
重定向的本质:Modify the file descriptor pointed toFILE* .
redirection and buffers:
下面我们来看看这个代码:
关闭了stdout,Then it was meant to bestdout写入的,重定向到了log.txt当中.
After making some corrections to the code:
Why didn't you write itlog里面?
Because the data is written to the file,Not operating directly on memory,Instead, the data needs to be written first缓冲区:
This is also known as input redirection;
缓冲区:The peripheral files are based on the Von Neumann system,效率低.It's a balanced approach.
1.无缓冲
2.行缓冲 以"\n"为标志 进行刷新
3.全缓冲 文件写入(磁盘)
缓冲区在哪里??
由谁提供?
The OS has buffers?What does it have to do with it?
The code below will answer:
Print strings on the screen using different interfaces.
添加fork();
Why redirect to log内,will become more?!
所以,缓冲区的概念,It is provided by the language.
The stream is opened by default 与 文件标识符:
stdout(FILE* )-----> 1(fd).
FILE是一个结构体 ,封装了fd
所以stdout-->FILE对象(fd=1) Only accept bids,But don't care about the content~
(4)文件管理
当进程运行,Call the file function,打开文件.Multiple processes can open multiple files.So we need an operating system,Manage these files.
管理的本质: 先描述 再组织.
//文件信息(内容+属性)
strcut file
{
int _fd;
}
进程与文件的关系:
磁盘文件vs内存文件
磁盘文件:内容+属性.
内存文件:More is attribute information.
(5)使用dup2:
简单来说,Just let the old descriptor 拷贝 新的文件描述符.即,对new重定向.
把fd1的内容 Overwrite write to1里面.
感谢你的阅读
祝你好运~
边栏推荐
- 所有子字符串中的元音 —— LeetCode - 2063
- 分割回文串 DP+回溯 (LeetCode-131)
- 【plang 1.4.3】定时器的使用
- 实现动态库(DLL)之间内存统一管理
- 进程(番外):自定义shell命令行解释器
- [Popular Science Post] I2C Communication Protocol Detailed Explanation - Partial Software Analysis and Logic Analyzer Example Analysis
- 联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料
- 【Arduino 连接 SD 卡模块实现数据读写】
- 【nRF24L01 与 Arduino 连接实现无线通信】
- 【plang1.4.3】编写水母动画脚本
猜你喜欢
uniCloud use
所有子字符串中的元音 —— LeetCode - 2063
与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
【plang1.4.3】编写水母动画脚本
【详解】线程池及其自定义线程池的实现
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
使用批处理脚本修改hosts文件
NE5532运放加法器
Case | industrial iot solutions, steel mills high-performance security for wisdom
随机推荐
Compatible with C51 and STM32 Keil5 installation method
【plang 1.4.3】定时器的使用
基于树莓派的智能箱包开发环境搭建
云服务器web项目部署详解
基础IO(上):文件管理和描述符
VCA821可变增益放大器
倍福ET2000侦听器使用
【科普贴】MDIO接口详解
【Arduino连接GPS 模块 (NEO-6M)读取定位数据】
【nRF24L01 与 Arduino 连接实现无线通信】
uniCloud通讯录实战
使用Vercel托管自己的网站
发布全新的配置格式 - AT
[Arduino uses a rotary encoder module]
408-二叉树-先序中序后序层次遍历
【科普贴】I2C接口详解——偏硬件解析
基础IO(下):软硬链接和动静态库
bluez5.50+pulseaudio实现蓝牙音响音频播放
如何使用 PHP 实现网页交互
联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料