linux IO操作

IO流程图

不加缓冲区—文件IO

直接运行

效率低

加缓冲区—标准IO

减少IO次数

高级

程序如何使用IO

区别

标准IO:
1.标准IO是由库函数系统提供的,由ANSI C标准定义
2.是带缓冲区的操作,运行效率较高
3.支持跨平台的
4.流指针实现

文件IO:
1.文件IO是由操作系统提供的,由POSIX(可移植操作系统接口)定义
2.没有缓冲区,运行效率没有标准IO高
3.不支持跨平台的
4.文件描述符实现

程序实现

文件描述符

系统调用,文件IO

本质是数组下标

指向文件file指针

流指针

库函数,标准IO

一个结构体:FILE

缓冲区指针+文件file指针

标准IO

缓存区

刷新:将缓冲区内容提交到文件

全缓冲

缓冲区满了才刷新,或者强制刷新(fflsh)

行缓冲

碰到\n,先刷新

一般printf,后面都加\n

为了防止死循环暂存缓存区,一直不输出

不缓冲

直接刷新

perror,不缓冲,直接刷新——可以直接刷新

打开关闭文件

fopen

fclose

定义
	FILE *fopen(const char *path, const char *mode);
包
	#include <stdio.h>
参数
	const char *path
		文件路径
	const char *mode
		打开模式
		
返回值
	EFIL结构体指针

打开模式

   r      Open text file for reading.  The stream is positioned at the beginning of the file.
   r+     Open for reading and writing.  The stream is positioned at the beginning of the file.

   w      Truncate file to zero length or create text file for writing.  The stream is positioned at the beginning of the file.

   w+     Open for reading and writing.  The file is created if it does not exist, otherwise it is truncated.  The stream is positioned at the beginning of the file.

   a      Open for appending (writing at end of file).  The file is created if it does not exist.  The stream is positioned at the end of the file.

   a+     Open  for  reading  and  appending (writing at end of file).  The file is created if it does not exist.  The initial file position for reading is at the beginning of the
          file, but output is always appended to the end of the file.

示例代码

int main(int avgc,char *avgc){
    FILE *f = fopen(avgc[1],'r');
    fclose(f);
}

操作文件

字符读写

行读写

结构体读写

格式化写

文件IO

操作系统底层接口

#include <stdio.h>

int main(int avgc,char *argv[]){
	
	int fr = open(argv[1],O_RDONLY);
	int fw = open(argv[2],O_WRONLY||);
	
}

猜你喜欢

转载自blog.csdn.net/qq_43537701/article/details/132593056
今日推荐