一、标准IO和系统IO的使用场景
系统IO:适用于打开设备文件(硬件文件) LCD、 触摸屏(效率比较高) ;可以打开硬件文件也可以打开普通文件
标准IO:适用于打开普通文件 ,比如txt png bmp jpg;无法去打开硬件设备文件
二、标准IO
系统IO:open read write close lseek
标准IO:fopen fread fwrite fclose fseek(标准库里面的函数 man 3)
三、标准IO的API (access prosess interface)
1.fopen 打开文件
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
参数:
path:文件路径名
mode:权限
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 cre‐
ated 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.
可读可追加,如果文件不存在就创建,读文件光标定位在文件头,追加文件光标定位在文件尾。
返回值:
成功:文件指针 FILE *
失败:NULL
总结:
带有‘r’:可读或者可读可写,需要手动创建
带有‘w’:可写或者可读可写,如果没有自动创建
带有'a' : 具有特殊功能的写(追加写),如果没有自动创建
mode变量的赋值要使用字符串,不是字符 。如"r" "r+" "w" "w+"