当前位置:网站首页>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里面.
感谢你的阅读
祝你好运~
边栏推荐
- 龙芯2K1000使用nfs挂载文件系统进行使用
- 【plang 1.4.5】编写坦克(双人)游戏脚本
- 【Popular Science Post】UART Interface Communication Protocol
- 写博客的原因。
- GM7150 CVBS转BT656视频解码芯片详细内容及设计要求
- 案例|工业物联网解决方案·智慧钢厂高性能安全数采
- 2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
- Personal image bed construction based on Alibaba Cloud OSS+PicGo
- 倍福ET2000侦听器使用
- 关于IIC SDA毛刺的那些事
猜你喜欢

MAC安装Mysql超详细完整教程

idea中创建jsp项目详细步骤

How to quickly build your own IoT platform?
![[Arduino connected to GP2Y1014AU0F dust sensor]](/img/b4/c32dcd32bf5b9e8596af406c9177a2.png)
[Arduino connected to GP2Y1014AU0F dust sensor]

Comparative analysis of mobile cloud IoT pre-research and Alibaba Cloud development

USB2.0一致性测试方法_高速示波器

【nRF24L01 与 Arduino 连接实现无线通信】

【多线程】线程安全保护机制

AD PCB导出Gerber文件(非常详细的步骤)

【Popular Science Post】Detailed explanation of MDIO interface
随机推荐
电子密码锁_毕设‘指导’
引擎开发日志:集成Bullet3物理引擎
R语言 —— 多元线性回归
Chrome 里的小恐龙游戏是怎么做出来的?
TeamCode 产品 UI 全新升级,快来体验吧
进程(中):进程状态、进程地址空间
向龙芯2K1000板子上烧写中标麒麟系统
阿里云华为云对比分析
IDEA2021.2安装与配置(持续更新)
引擎开发日志:OpenGL资源多线程加载
VCA821可变增益放大器
物联网方案
C语言教程 - 制作单位转换器
基于阿里云OSS+PicGo的个人图床搭建
回溯法 & 分支限界 - 2
AD8307对数检波器
2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
本地数据库 sqlite3 编译和使用
倍福ET2000侦听器使用
开源代码交叉编译操作流程及遇到的问题解决(lightdm)
