当前位置:网站首页>The difference between open and fopen
The difference between open and fopen
2022-07-22 03:06:00 【Amos_ zh】
1. Function declaration 、 Parameters 、 Return value 、 The header file
Function declaration :
FILE *fopen(const char *filename, const char *mode)
Parameters :
1. filename -- This is a C character string , Contains the name of the file to open .
2. mode -- This is a C character string , Including file access mode , The pattern is as follows :
"r" Open a file for reading . The file must exist .
"w" Create an empty file for writing . If the file name is the same as an existing file , The contents of the existing file will be deleted , The file is treated as a new empty file .
"a" Append to a file . The write operation appends data to the end of the file . If the file doesn't exist , Create file .
"r+" Open a file to update , Can be read or written to . The file must exist .
"w+" Create an empty file for reading and writing .
"a+" Open a file for reading and appending .
Return value :
This function returns a FILE The pointer . Otherwise return to NULL, And set the global variable errno To identify errors .
The header file :
#include <stdio.h>
open
Function declaration :
int open( const char * pathname,int flags);// Open an existing file
int open( const char * pathname,int flags, mode_t mode);// Open a file that doesn't exist
Parameters :
- pathname: The file path string to open .
- flag:flags The parameter has a series of constant values to choose from , You can select multiple constants at the same time and connect them with bitwise OR operators , So the macro definitions of these constants are based on O_ start , Express or.
(1) Will options , One of the following three constants must be specified , And only one... Is allowed to be specified .
O_RDONLY Read only open
O_WRONLY Just write open
O_RDWR Read and write open
(2) optional , At the same time specified 0 One or more , And the required options are bitwise OR
O_CREAT If this file does not exist, create it . When using this option, you need to provide the third parameter mode, Indicates the access rights of the file .
O_EXCL If you also specify O_CREAT, And the file already exists , Error return .
O_TRUNC If the file already exists , And open in write only or read-write mode , Then its length is truncated (Truncate) by 0 byte .
O_NONBLOCK For device files , With O_NONBLOCK Mode open can do non blocking I/O(Nonblock I/O).
- mode: Specify the file permissions when creating a new file
7( Readable, writable, and executable )、4( Can be read )、2( Can write )、1( Executable )
Return value :
If successful, the descriptor of the open file is returned , Failure returns -1.
The header file :
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
2. About the difference between buffers ( The main )
- fopen In user mode, there is a cache , It has been used. FILE This structure holds buffered data . It's going on read and write Reduce the switching between user mode and kernel mode .
- open There is no cache , Each read operation obtains data directly from the file system . It's going on read and write You need to switch between kernel mode and user mode every time .
- If you access files sequentially ,fopen A series of functions is better than calling open Series fast ; If you randomly access files open than fopen fast .
3. Different levels
- open It's a system call , The file handle is returned ( File descriptor ), The handle of a file is the index of the file in the file description sub table .open It's a system function , It's not portable .
- fopen yes ANSIC Standard C Library functions , Returns a pointer to the file structure . Different kernels should be called in different systems api, portable .
- linux The system function in is open,fopen Is its encapsulation function ,fopen Implementation of to call open.
4. Different scenarios
- It's usually used fopen Open a normal file , use open Open device file .
边栏推荐
- Mysql进阶2
- pytorch学习之nn.Sequential类—使用Sequential类来自定义顺序连接模型
- 博物馆数字化——数字藏品app开发
- bootloader系列二——arm920t--bootloader架构设计
- 【Mysql】忘记密码怎么办?
- Bootloader series 4 - clock initialization
- 2019 Hangdian multi school sixth session 6641 (original 1008) TDL (regular question)
- Unity2D~对周目解密小游戏练习(三天完成)
- 2019杭电多校 第一场 6581-Vacation【思维】
- WDK开发入门1-基础环境搭建和第一个驱动程序(VS2010)
猜你喜欢
文字检测篇-传统篇
Don't know a little statistics, "Star Wars" for nothing
bootloader系列三——核心初始化
pytorch入门三 数据类型与函数
Clear DNS cache on local computer + clear DNS cache on Browser
bootloader系列一——Arm处理器启动流程解析
A Beginner guide to Deep Learning based Semantic Segmentation using Keras
2019 Hangdian multi school sixth session 6641 (original 1008) TDL (regular question)
Introduction to pytorch 3 data types and functions
博物馆数字化——数字藏品app开发
随机推荐
接口测试经典面试题:Session、cookie、token有什么区别?
There are 8778 authors in a paper: each person writes 5 words, and the signature takes 17 pages
动态创建对象时候为什么使用new,而不是malloc
VC字符串与时间戳相互转换
tslib-1.4移植到mini2440开发板
871. 约数之和
C: File encryption
2019杭电多校 第五场 6630(原1007) permutation 2 (斐波那契数列)
2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790
Interpretation of openmmlab series framework (based on pytorch)
matplotlib绘制直线图
开启关闭线程的封装类
pytorch入门三 数据类型与函数
Code representation pre training language model learning guide: principles, analysis and code
Clear DNS cache on local computer + clear DNS cache on Browser
【论文笔记】Modeling Task Relationships in Multi-task Learning withMulti-gate Mixture-of-Experts
本机电脑清除DNS缓存+浏览器清除DNS缓存
Codeforces round 578 (Div. 2) C - round corridor [number theory + Law]
linux使用yum快速安装mysql
matplotlib.pyplot接口汇总